Skip to content

Instantly share code, notes, and snippets.

Created December 30, 2013 07:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/8178845 to your computer and use it in GitHub Desktop.
Save anonymous/8178845 to your computer and use it in GitHub Desktop.
Expando Object sample code
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DynamicObjectExample
{
class Program
{
static void Main(string[] args)
{
dynamic d = CreateDynamicObject2();
Console.WriteLine("Prop1: {0}", d.Prop1);
Console.WriteLine("Prop2: {0}", d.Prop2);
Console.WriteLine("Prop3: {0}", d.Prop3);
d.InvokeVoid();
var e = d.InvokeAdd(1, 2);
Console.WriteLine("e={0}", e);
Console.Read();
}
static dynamic CreateDynamicObject()
{
dynamic d = new ExpandoObject();
// assign property
d.Prop1 = 1;
d.Prop2 = 2;
d.Prop3 = 3;
// assign void method.
d.InvokeVoid = new Action(() => Console.WriteLine("InvokeVoid."));
// assign returnable method.
d.InvokeAdd = new Func<int, int, int>((a, b) => a + b);
return d;
}
static dynamic CreateDynamicObject2()
{
dynamic d = new ExpandoObject();
IDictionary<string, object> item = d as IDictionary<string, object>;
// assign property
item.Add("Prop1", 1);
item.Add("Prop2", 2);
item.Add("Prop3", 3);
// assign void method.
item.Add("InvokeVoid", new Action(() => Console.WriteLine("InvokeVoid.")));
// assign returnable method.
item.Add("InvokeAdd", new Func<int, int, int>((a, b) => a + b));
return d;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment