C # Expando Fun

Uma das coisas mais legais que vi com o .Net Framework foi como você pode usar ExpandoObjects para todos os tipos de coisas. Vindo de um cara que gosta da flexibilidade de Python ou JavaScript, esse tipo de coisa aquece meu coração.

// you'll need to ref System.Dynamic
dynamic d = new System.Dynamic.ExpandoObject();

d
.MyEvent = null; // needs to be initialized this way before assignment
d
.MyEvent += new EventHandler((sender, args) => Console.WriteLine("event one"));
d
.MyEvent += new EventHandler((sender, args) => Console.WriteLine("event two"));

d
.Console = new Action(() => Console.WriteLine("action!"));
d
.Multiply = new Func<int, int>((i) => i * i);

d
.Prop = "string property";
Console.WriteLine(d.Prop);
d
.Prop = 1;
Console.WriteLine(d.Prop);

EventHandler e = d.MyEvent;
e
(d, EventArgs.Empty);
d
.Console();
Console.WriteLine(d.Multiply(2));