.NET Dynamic Objects using ExpandoObject class

Microsoft .Net provides (Framework 4) support to deal with dynamic objects. Using ExpandoObject class we can work with dynamic types. ExpandoObject class represents an object whose members can be dynamically added and removed at run time. Means we can add or remove any member in runtime of its instance also we can set the values for members.

As this is dynamic object so we need to take any burden about design time definition, means no need to define any entity in design time. We can add the properties dynamically. Now check now can we deal with dynamic object using ExpandoObject class.

1. Adding properties

//Adding properties
        private void btnAdd_Click(object sender, EventArgs e)
         {
             try
            {
                dynamic objEmployee;
                objEmployee = new System.Dynamic.ExpandoObject();
                objEmployee.ID = "101";
                objEmployee.Name = "Ramesh";
                objEmployee.Address = "Prem Nagar";
                objEmployee.DeptID = "1";
                dgvResult.DataSource = ((IDictionary<String, Object>)objEmployee).ToList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

In above code, you can see that I have created the instance of ExpandoObject by named – “objEmployee”. Here I have added dynamic properties – ID,Name,Address and DeptID.

These are properties are dynamic also I have set he values for these properties.

2. Enumerating properties


//Enumerating properties
        private void btnEnumerate_Click(object sender, EventArgs e)
         {
             try
            {
                dynamic objEmployee;
                objEmployee = new System.Dynamic.ExpandoObject();
                objEmployee.ID = "102";
                objEmployee.Name = "Krish";
                objEmployee.Address = "Moti Nagar";
                objEmployee.DeptID = "2";
                foreach (var property in (IDictionary<String, Object>)objEmployee)
                {
                    MessageBox.Show(property.Key + "- " + property.Value);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

After adding these properties to dynamic object we can iterate the object properties. To iterate these properties I have used foreach loop, which will print the property name and corresponding values.

3. Removing properties

  //Removing properties
        private void btnRemove_Click(object sender, EventArgs e)
         {
             try
            {
                dynamic objEmployee;
                objEmployee = new System.Dynamic.ExpandoObject();
                objEmployee.ID = "103";
                objEmployee.Name = "Anant";
                objEmployee.Address = "Basoda";
                objEmployee.DeptID = "3";
                ((IDictionary<String, Object>)objEmployee).Remove("DeptID");
                dgvResult.DataSource = ((IDictionary<String, Object>)objEmployee).ToList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

Like we have added the properties to dynamic object we can also delete the properties. You can see the deletion code in above.
Now the most important thing, we can also add the event for dynamic object, check the below implementation-

4. Adding event

//Adding event
        private void btnAddEvent_Click(object sender, EventArgs e)
         {
             try
            {
                dynamic objEmployee;
                objEmployee = new System.Dynamic.ExpandoObject();
                objEmployee.ID = "104";
                objEmployee.Name = "Chetan";
                objEmployee.Address = "Bhatera";
                objEmployee.DeptID = "4";

                 //Binding handler
                objEmployee.EmployeeEvent = null;
                objEmployee.EmployeeEvent += new EventHandler(EmployeeHandler);

                 // Raising event.
                objEmployee.EmployeeEvent(objEmployee, new EventArgs());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

       //Handler
        static void EmployeeHandler(object sender, EventArgs e)
         {
             //Do your work with sender
            MessageBox.Show("Do your work here");
        }

I above code, you can see I have added EmployeeEvent to dynamic object and also handler for it-“EmployeeHandler”. To call this handler I have used below line of code-

objEmployee.EmployeeEvent(objEmployee, new EventArgs());

When this line of code will be executed then the code which is written in EmployeeHandler() handler will be triggered.

Note-

A variable of dynamic type can point to different types at runtime.Means yo can differet type of values at runtime.

Ex-


objEmployee.ID = "104";
objEmployee.ID = 104;

To get more details about ExpandoObject class, you can check this l ink also-

http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject%28v=vs.110%29.aspx

As per the Microsoft documentation –

The ExpandoObject class implements the standard Dynamic Language Runtime (DLR) interface IDynamicMetaObjectProvider, which enables you to share instances of the ExpandoObject class between languages that support the DLR interoperability model.

You can download the code from here-

Dynamic object usingExpandoObject

By Jitendra Faye   Popularity  (2209 Views)