Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding methods to ExpandoObjects

UPDATE

The problem is not the code, the problem is that you apparently can't evaluate dynamic objects from the immediate window.


I'm trying to tack on methods to an ExpandoObject but not sure how to get it to work. Here's my code:

dynamic myObj = new ExpandoObject();
myObj.First = "Micah";
myObj.Last = "Martin";
myObj.AsString = new Func<string>(() => myObj.First + " " + myObj.Last);

//No matter what I do I get 'object' does not contain a definition for 'AsString'
myObj.AsString;
myObj.AsString();
myObj.AsString.Invoke();

Anyone know how to do this?

like image 605
Micah Avatar asked Sep 16 '10 14:09

Micah


1 Answers

Are you sure you included all the code?

I just tested and ran the following and was successful:

dynamic obj = new ExpandoObject();

obj.First = "Hello";
obj.Last = "World!";

obj.AsString = new Func<string>(() => obj.First + " " + obj.Last);

// Displays "Hello World!"
Console.WriteLine(obj.AsString());
like image 157
Justin Niessner Avatar answered Oct 13 '22 05:10

Justin Niessner