c# - Dynamic and ExpandoObject

ExpandoObject as we have introduce in the  "C# - DynamicObject with Dynamic and create wrapper with DynamicObject " section in that it allows dynamically add/remove properties. and in tihs post, we will going to examine the attributes of ExpandoObject more closely.

In general, ExpandoObject supports dynamic binding, which enables you to use Standard syntax such as sampleObject.sampleMember instead of more complext syntax like sampleObjects.GetAttribute("sampleMember");

You have to create instance of ExpandoObject with dynamic keyword. 

you can do the following with an expandoObject. 

  • Add new Members
  • Add method (in the form of delegate) 
  • Adding Events.(this is super cool)
  • passing as Parameter (dynamic as the sole type of the parameter) 
  • Enumeraing and Deleting Members
  • Receiving notification of property Changes. 

Below is the code to show various aspect of ExpandoObject, and you may find more information on the Expando from the MSDN introduction .


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic;

namespace ExpandoObjectDemo
{
    class Program
    {
        static void Main(string[] args)
        {

            dynamic sampleObject = new ExpandoObject();
            
            // Adding new members
            sampleObject.test = "Dynamic Property";
            Console.WriteLine(sampleObject.test);
            Console.WriteLine(sampleObject.test.GetType());

            //adding complex data member (data types other than primitive types such as string)
            sampleObject.number = 10;
            sampleObject.Increment = (Action) (() => { sampleObject.number++; });
            Console.WriteLine(sampleObject.number);
            sampleObject.Increment();
            // After calling the Increment methods.
            Console.WriteLine(sampleObject.number);

            // Add an event to an instance of the ExpandoObject class
            sampleObject.sampleEvent = null; // the chanllenge of event is that you cannot specify the type of the Event when you declare it 
            sampleObject.sampleEvent += new EventHandler(SampleHandler); // how does it knows that sampleEvent is an Event? -- I Guess this must be late bound and the place holding sampleEvent might change depending on the context
            sampleObject.sampleEvent(sampleObject, new EventArgs());

            // Passing Parameter
            dynamic employee, manager;
            employee = new ExpandoObject();
            employee.Name = "John Smith";
            employee.Age = 33;

            manager = new ExpandoObject();
            manager.Name = "Allison Brown";
            manager.Age = 42;
            manager.TeamSize = 10;
            
            WritePerson(manager);
            WritePerson(employee);


            // Enumeraing and deleting members
            // you can cast the ExpandoObject to an instance of IDictionary<string, object>
            foreach (var property in (IDictionary<string, Object>) employee)
            {
                var key = property.Key;
                var value = property.Value;
                Console.WriteLine(key + ":" + value);
            }

            // also, you can remove items after casting back to IDictionary<string, object>
            ((IDictionary<string, object>) employee).Remove("Name");

            // you can receive notification on property changes
            ((INotifyPropertyChanged) employee).PropertyChanged += new PropertyChangedEventHandler(HandlePropertyChanges);
            employee.Name = "John Smith";
        }

        static  void SampleHandler(object sender, EventArgs e)
        {
            Console.WriteLine("SampleHandler for {0} event", sender);
        }

        // show passing as parameters
        private static void WritePerson(dynamic person)
        {
            Console.WriteLine("{0} is {1} years old.", person.Name, person.Age);

            try
            {
                // the following shall cause an exception , if object passed in is a Person object
                Console.WriteLine("Manager {0} people", person.TeamSize);
            }
            catch
            {
                // Ignore
            }

        }

        private static void HandlePropertyChanges(object sender, PropertyChangedEventArgs e)
        {
            Console.WriteLine("{0} has changed. ", e.PropertyName);
        }
    }
}

 if you want to find some references , here it is

 

ExpandoObject class

 

 

你可能感兴趣的:(C#)