Created
December 30, 2013 07:16
-
-
Save anonymous/8178845 to your computer and use it in GitHub Desktop.
Expando Object sample code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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