Tuesday, May 18, 2010

Silverlight 4.0, Dynamic Types & Extension Methods

I’ve been very busy lately earning my living.  Not able to get time to blog!  But hey, I wanted to hack together something - the DynamicMvvM framework which I had hinted about earlier - over this weekend.  This got me working with the beauty that is DynamicObject (and ExpandoObject) in C# 4.0.

The first error I had hit was probably the one every one working with DynamicObject for the first time encounters:

Error    2    Predefined type 'Microsoft.CSharp.RuntimeBinder.Binder' is not defined or imported    TestApp
Error    3    One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll?   

True enough when I did add Microsoft.CSharp.dll and System.Core.dll, the errors went away.

Using a keyword in C# requires me to add some assemblies?  Oops…  Adding these two assemblies to Visual Studio project templates for Silverlight, by default, would be high on my wish list!  Thankfully, these are added by default to WPF projects (the only one I checked) and hopefully other project types.

Another interesting thing about DynamicObject is that when it’s assigned to a dynamic, it’s not able to find ExtensionMethods.  This is reasonable considering all the calls to the methods and properties are resolved at runtime and the ExtensionMethods are not part of the object’s interface per se.

Just one one more piece of info you need to file away for future use.