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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值