To use DLR DynamicObject/ExpandoObject or not ???

Sep 24, 2009 at 3:13 PM

DLR community,

Hi.  I've been away from dynamic languages for almost 20 years - having done awk, c-shell, and perl on UNIX in an earlier life.  I have spent the bulk of my time designing and coding strongly typed, high-perf server logic in C# and before that, COM/C++ - all in MS technologies.  So, I'm looking for some advice from the dynamic experts.  Let me explain what I'm trying to build.  Based on that, I'd like to know if it's worth considering the DLR's DynamicObject/ExpandoObject, or if it's not really going to benefit me over a static c# implementation using my own lookup tables, etc..

Goal: Provide a concrete c# implementation of a dynamic object for creating new "types" in our system.  This class will be the basis for all of our objects - as we want pre-built objects to be able to have props/events/methods added dynamically to their type definition.  In addition, users can create completely new "types" in our system and define their shape from scratch using this dynamic object.  To enable this, there will be a UI for configuring the new or existing "types" that allows:

  • adding strongly typed properties (i.e. basically, creating an extensible collection of properties) that support WPF's data-binding model
  • adding strongly typed events to the type
  • adding method bodies in the form of [<type> METHOD (var args)]
  • this might be the kicker - i want to save this configuration of my dynamic object as a new "type" and use it at both design time with [ideally] intellisense and at runtime
  • all this must be done [without] using reflection.emit and generating assemblies

After reading through the docs on the DLR, watching the PDC 08 deep-dive session and looking at DynamicObject and ExandoObject, I have a basic question:  "is it worth spending the time on, or not?" 

If my goals are not in line with what DynObj/ExObj were designed to do, then I will save my time and focus on other things.  But, if these classes were designed to allow me to build a concrete object that enables dynamically creating types that can be persisted and re-used, then it's definitely worth prototyping.

Thanks for your time and please let me know if you would like further clarification on what I am looking to do,

Bob DeRemer

Sep 25, 2009 at 8:21 PM

Hi Bob,

Much of what you’re describing is exactly the kind of scenario that DynamicObject is designed to enable. The main sticking point as you guessed, is this:

  • this might be the kicker - i want to save this configuration of my dynamic object as a new "type" and use it at both design time with [ideally] intellisense and at runtime

Intellisense would work on any properties that were defined in C#, but it won’t show anything that was added dynamically. (Technically speaking, intellisense just displays assembly metadata, or for C# source files, the information that will eventually end up in the assembly’s metadata). To get intellisense at design time, you would have to persist the properties/methods, either to some assembly or to C# source files.

Also:

  • All this must be done [without] using reflection.emit and generating assemblies

DLR doesn’t require use of AssemblyBuilder in Reflection.Emit. But it does generate DynamicMethods under the hood, which are in the ref.emit namespace, but they’re much lighter weight than the *Builder classes in ref.emit.

Hope this helps,

- John Messerly