How to add dynamic property to your class with ExpandoObject<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

How to add dynamic property to your class with ExpandoObject

Posted on: December 5, 2012

In some scenario, you may want to add dynamically information to your object without having to define a specific type or interface. This type of scenario has been developed by Asp.Net MVC with the ViewBag which let you add any type of information dynamically. In a project, you may want to have this kind of behavior and be able to add information of different type and to access it later.

To do, you need to use the ExpandoObject.

1public class YourClass {
2 public int Id{ get; set; }
3 public dynamic Attributes { get; set; }
5 public MatriceRow() { Attributes = new ExpandoObject();
6 }

This is a working example. From there you can use the YourClass class and set attributes.

1var myClass = new YourClass();
2myClass.Id = 1;
3myClass.Attributes.IsBoolean = true;
4myClass.Attributes.MyName = "Patrick Desjardins";

You cannot define the Attributes property directly to the type of ExpandoObject. If you do, you will get an exception.

'IsBoolean' and no extension method 'IsBoolean' accepting a first argument of type 'System.Dynamic.ExpandoObject' could be found (are you missing a using directive or an assembly reference?)

This functionality should be used rarely. It's always better to be strongly typed and not use dynamic because this one is only checked when the code is executed rather than when compiled.