2010/12/12

New "dynamic" Keyword in C# 4

Instead of doing this:

object calc = GetCalculator();
Type calcType = calc.GetType();
object res = calcType.InvokeMember("Add",
    BindingFlags.InvokeMethod, null,
    new object[] { 10, 20 });
int sum = Convert.ToInt32(res);

Do this now:

dynamic calc = GetCalculator();
int sum = calc.Add(10, 20);

Thanks to Scott Hanselman by his post.