The ExpandoObject
represents an object whose members can be dynamically added and removed at run time.
myObject.MyMember
instead of more complex syntax like myObject.GetAttribute("MyMember")
.The following example shows the basic usage of ExpandoObject
.
public static void Example1()
{
string script = @"
dynamic customer = new System.Dynamic.ExpandoObject();
customer.Name = ""Mark Upston"";
customer.Age = 53;
customer.Address = ""New York, US"";
Console.WriteLine(""{0} is {1} years old and lives in {2}"", customer.Name, customer.Age, customer.Address);
";
ExpressionEvaluator evaluator = new ExpressionEvaluator();
Console.WriteLine(evaluator.ScriptEvaluate(script));
}
In C#, to enable late binding for an instance of the ExpandoObject
class, you must use the dynamic
keyword.
The ExpandoObject
class also allows you to define and use methods, as shown below.
public static void Example2()
{
string script = @"
dynamic myObject = new ExpandoObject();
myObject.Add = (a, b) =>
{
return (a + b).ToString();
};
Console.WriteLine(myObject.Add(13, 42));
";
ExpressionEvaluator evaluator = new ExpressionEvaluator();
Console.WriteLine(evaluator.ScriptEvaluate(script));
}