Click here to Skip to main content
13,355,335 members (72,862 online)
Click here to Skip to main content
Add your own
alternative version


12 bookmarked
Posted 25 Oct 2010

An introduction to Expando Object- Dotnet 4.0

, 27 Oct 2010
Rate this:
Please Sign up or sign in to vote.
This short article will highlight upon some of the features of Expando Object


One more addition to the dotnet 4.0 framework is the Expando object. It is defined in the System.Core assembly and belongs to System.Dynamic namespace.

Intellisense speaks of Expando object as


Expando Object – An Inside View

A detailed look inside the Expando Object reveals the following


The Expando Class cannot be inherited as it is Sealed. It implements six different interfaces. Out of which the below two needs special attention


We know that any object that can have its operations bound at runtime must implement the IDynamicMetaObjectProvider. So is the Expando object.

Hence dynamic dyn = new ExpandoObject(); is a perfectly valid statement.


Whenever a new member is added or modified, the class raises the PropertyChange Event.

Using the code

Example 1: Adding members

Consider the below program

dynamic dyn = new ExpandoObject();
//Members additon
dyn.Member1 = "Some Member";
dyn.Member2 = 2;

Simply we added two members to our ExpandoObject. Really simple.

Example 2: Reading and Writing from Expando Collection

This code piece will show how we can assign values to an Expando Collection

List<expandoobject /> expandoList = new List<expandoobject />();

//Writing to Expando Collection
for (int i = 2; i --> 0;)
  dynamic dynObject = new ExpandoObject();
  dynObject.Prop1 = i;
  dynObject.Prop2 = "String Property" + i;                
  dynObject.Prop3 = AddDictionaryObject (i);

//Adding a dictionary object
private static Dictionary<int, /> AddDictionaryObject(int i)
   Dictionary<int, /> dict = new Dictionary<int, />();
   dict.Add(i, "Value" + i.ToString());
   return dict;

First we are instantiating a new ExpandoObject collection.

List<expandoobject /> expandoList = new List<expandoobject />();

Then by using the new “Goes To” operator, we have populated two items into the Collection.

The line dynamic dynObject = new ExpandoObject(); ensures that the ExpandoObject is assigned to the dynamic object and henceforth we can add properties to it dynamically which the DLR will check at runtime. We also have a property(here Prop3) which is of type Dictionary < int, string >

The code is pretty straight forward.

The next part shows how to READ values from the Expando Collection just created

//Reading from Expando Collection

foreach (dynamic d in expandoList)
   Console.WriteLine("Reading Collection {0} Values", d.Prop1);
   Console.WriteLine("Prop1 value is:   {0}",d.Prop1);
   Console.WriteLine("Prop2 value is:   {0}", d.Prop2);
   foreach (dynamic dic in d.Prop3)
     Console.WriteLine("The key is {0} and it's value is {1}",dic.Key,dic.Value);


As can be understandable that we are looping through the Expando Collection and since every item is of type dynamic, we can simply call the Property name. However, whether the property is valid or not will be checked at runtime.

Example 3: Adding Action and Func delegates

Consider the below

Case 1: Add Action Delegate for a Callback

dynamic dynObj= new ExpandoObject();

//Add Action Delegate
dynObj.Action = new Action(() =>
   Console.WriteLine("Helo From Action Delegate");
//Invoke the Action method

Nothing much so..Simply assigning the Action delegate at runtime and Invoking the same for a simple callback.

Case 2: Add Func Delegate for a Callback

//Add Func delegate 
dynObj.Func = new Func<int, />                
              ((a, res, str) => Calci(a,res,str));

//Invoke the Func Method
Console.WriteLine("The {0} operation yields {1}", 
				"+", dynObj.Func(12, 4, "+"));

And the Calci Function is here

private static int Calci(int num1, int num2, string op)
  int result = 0;
  switch (op)
    case "+":
    result = num1 + num2;
     case "-":
     result = num1 - num2;
     case "*":
     result = num1 * num2;
     case "/":
     result = num1 / num2;

   return result;

This shows how we can use the Func delegate to make simple Arithmetical operations.

Example 4: Notifying Property Change events

As mentioned above , the Expando Object implements the INotifyPropertyChanged interface which is in System.ComponentModel namespace.

That means, whenever we add a new Property to the Expando Object or Change the Value of the existing Property, the PropertyChanged event handler should get trigger. Lets see how it can be done

dynamic dynObject = new ExpandoObject();
 ((INotifyPropertyChanged)dynObject).PropertyChanged += 
 new PropertyChangedEventHandler(Program_PropertyChanged);

dynObject.Property1 = 10;

//Now change the value
dynObject.Property1 = 20;

And the PropertyChanged Event handler is as under

static void Program_PropertyChanged
	object sender
	, PropertyChangedEventArgs e
	Console.WriteLine("Property {0} with value {1}",
					e.PropertyName, ((dynamic)sender).Property1);

Here we have simply define a property Property1 and set the initial value to 10. Next we change the property value to 20.The result is


Example 5: Handling events

We can define events to our ExpandoObject.

The steps to follow is listed as under

#region Handling Events
 dynamic dynObjectEvent = new ExpandoObject();
 //Just Adding a memeber
 dynObjectEvent.Value = 10;

 //Step 1: Add a member and set it to null first
 dynObjectEvent.customEventMember = null;
 //Step 2:Now subscribe
 dynObjectEvent.customEventMember += new EventHandler(CustomEventHandler);
 //Step 3: Raise the Event
 EventHandler eh = dynObjectEvent.customEventMember;
 if (eh != null) eh.Invoke(dynObjectEvent, EventArgs.Empty);
//or eh(dynObjectEvent, EventArgs.Empty);


The event handler is defined as under/p>

static void CustomEventHandler(object sender, EventArgs e)
 Console.WriteLine("Custom event handler raised by {0} and has send a value {1}",
                , ((dynamic)sender).Value);

As can be seen that defining events is a three step prosess.

Step 1: Add a member and set it to null first

dynObjectEvent.customEventMember = null;

Step 2:Now subscribe the event

	+= new EventHandler(CustomEventHandler);

Step 3: Raise the Event

EventHandler eh = dynObjectEvent.customEventMember;
if (eh != null) eh.Invoke(dynObjectEvent, EventArgs.Empty);

The output being



In this short tutorial we have seen some of the benifits of Expando Object.

Comments on the topic are highly appreciated for the improvement of the topic.

Thanks for reading the article.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

You may also be interested in...

Comments and Discussions

GeneralMy vote of 4 Pin
Rudra rafiq17-May-11 1:18
memberRudra rafiq17-May-11 1:18 
GeneralMy vote of 1 Pin
Batzen29-Oct-10 8:17
memberBatzen29-Oct-10 8:17 
GeneralMy vote of 1 Pin
SledgeHammer0127-Oct-10 8:22
memberSledgeHammer0127-Oct-10 8:22 
General[My vote of 1] Not a great article Pin
CIDev26-Oct-10 7:39
memberCIDev26-Oct-10 7:39 
GeneralMy vote of 1 Pin
Selvin26-Oct-10 5:55
memberSelvin26-Oct-10 5:55 
GeneralMy vote of 1 Pin
Sacha Barber25-Oct-10 6:18
mvpSacha Barber25-Oct-10 6:18 
GeneralMy vote of 1 Pin
Argyle4Ever25-Oct-10 5:14
memberArgyle4Ever25-Oct-10 5:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 27 Oct 2010
Article Copyright 2010 by Niladri_Biswas
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid