.Net Tips and Tricks

Blog archive

Using Dynamic Classes to Pass Data

In an earlier tip on using tuples to return multiple values from a method, several readers noted that the tuple's property names (Item1, Item2) left a lot to be desired -- you couldn't really call those names "self-documenting". As a fan of Really Obvious Code (ROC Rocks!), I should point out that there's a solution: using a Dynamic class. As with tuples, the goal here is to create a way to return multiple values from a method without defining a whole new class with its attendant properties and then using that class in exactly one place (returning values from a method) for just a few milliseconds.

A dynamic class lets you add properties to a class without defining them beforehand, as in this example which defines the variable returnClass as dynamic and then adds a CustomerID and Age property to it just by setting values on those properties:

private dynamic GetCustomerInfo()
  {
    dynamic returnClass;
    returnClass.CustomerID = "A123";
    returnClass.Age = 21;
    return returnClass;
  }

To call this method, accept the class, and access the properties, you'd write code like this:

 
dynamic aClass;
aClass = GetCustomerInfo();
this.CustIdTextBox.Text = aClass.CustomerID;

What you give up with Dynamic classes is IntelliSense: when accessing the properties on aClass, IntelliSense won't provide the list of property names for the class. However, compared to using tuples (with the distinctly unhelpful names Item1, Item2 and so on), that's not a real loss. What you do lose that you may care about, however, is compile-time type safety: if you set a dynamic property to a string and then attempt to move the value into an integer variable, you won't find about your problem until the code executes at runtime.

As another reader pointed out in that original column, neither technique is a good choice for a public interface, but worth considering when you have two private methods interacting.

Posted by Peter Vogel on 02/13/2012 at 1:21 PM


comments powered by Disqus

Reader Comments:

Wed, Feb 15, 2012 Peter Vogel Canada

Nothing wrong with a hash (that I can think of). Personally, I don't like passing strings as keys to collections because the compiler can't check my spelling. But, then, using a dynamic or an anonymous class has the same problem: no IntelliSense support or compile time checking. However, I think that I prefer using tuples (which started this discussion) and dynamic classes because they involve less typing/fewer punctuations marks: just type the period after the variable name and enter your key. No (" or ") required. And, just because there's 21 ways of doing something, it doesn't mean that number 1 should be everyone's choice :).

Wed, Feb 15, 2012

Have you people never heard of a hash? What is this dynamic class, other than a set of key/value pairs. If there's 20 ways to do something, you guys will always think the 21st is such a good idea.

Tue, Feb 14, 2012 Peter Vogel Canada

Richard: You're right--I was so intent on showing how to set and return the data that I never bothered to initialize the class. I hadn't considered using an anonymous class because I wasn't sure what to declare the return type of the method--but using dynamic as the return type of the method lets me do that.

Tue, Feb 14, 2012 Richard

Your example doesn't work - you never assign the "returnClass" variable.

The simplest option is to use an anonymous type:

return new
{
CustomerID = "A123",
Age = 21,
};

Alternatively, change the first line of your method to:

dynamic returnClass = new System.Dynamic.ExpandoObject();

Tue, Feb 14, 2012 Peter Vogel Canada

Yes--dynamic classes are terrifically useful in ASP.NET MVC. In addition to using them to pass data from the controller to the View, it also makes it very easy, in TDD, to check the results of a method that returns a JsonResult: just load the JsonResult's Data property into a dynamic class and you can access the properties on the JSON object!

Mon, Feb 13, 2012

The MVC framework makes good use of dynamic objects, especially when passing data between a controller and view

Add Your Comments Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Comment:
Please type the letters/numbers you see above