Origami Robot

Nov 17
Permalink

Dynamic Binding in C# 4.0

speaker: Mads Torgersen, C# Program Manager at Microsoft.

Common Language Runtime was developed for static languages to interop together. Dynamic Language Runtime sits on top of CLR for dynamic languages to interop.

Dynamic Object : Implements its own bindings. Knows it self programatically. Leaves the responsibility of calling methods and properties to the object itself as opposed to the .NET runtime.

Driving force behind building the DLR is to allow dynamic languages access to the .NET framework as well as bring dynamicism to statically typed languages.

using System.Dynamic;
dynamic calc = GetCalculator();
int result = calc.Add(1, 1);

dynamic is a type recognized by the compiler that signifies the object will be doing its own “Method Dispach”.  The compiler wont care about anything you call because it will just assume you know what you’re doing.

System.Dynamic.ExpandoObject is a type that allows you to magically create properties just by assigning them.

dynamic d = new ExpandoObject();
d.Foo = “Bar”;

System.Dynamic.DynamicObject : a base type you can use to inherit from and create your own dynamic objects. Provides overridable methods you can use to replace compiler type functions. Using this type of object you intercept what the compiler would be doing at compile time, and handles it at runtime on its own.

*whenever a dynamic object is passed as a parameter to a statically typed method the resolution isnt resolved until runtime.