Implementing Dynamic Interfaces

系统 1856 0

名词解释

DLR: dynamic language runtime.

refer to this article: http://msdn.microsoft.com/en-us/vstudio/ff800651.aspx

下面是一个简单的实现,会动态的创建需要的属性:

      class Program

    {

        static void Main(string[] args)

        {

            dynamic dynamicProperties = new MyDynamicObject();



            try

            {

                Console.WriteLine(dynamicProperties.Marker);

            }

            catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException)

            {

                Console.WriteLine("There is no such a property.");

            }



            dynamicProperties.Date = DateTime.Now;

            dynamicProperties.Name = "Bill Wagner";

            dynamicProperties.Title = "Effective C#";

            dynamicProperties.Content = "Building a dynamic dictionary";



            Console.WriteLine(dynamicProperties);

            Console.ReadLine();

        }

    }



    class MyDynamicObject : DynamicObject

    {

        private Dictionary<string, object> storage = new Dictionary<string, object>();

        public override bool TryGetMember(GetMemberBinder binder, out object result)

        {

            if (storage.ContainsKey(binder.Name))

            {

                result = storage[binder.Name];

                return true;

            }

            result = null;

            return false;

        }



        public override bool TrySetMember(SetMemberBinder binder, object value)

        {

            string key = binder.Name;

            if (storage.ContainsKey(key))

                storage[key] = value;

            else

                storage.Add(key, value);



            return true;

        }



        public override string ToString()

        {

            var sb = new StringBuilder();

            foreach (var item in storage)

                sb.AppendLine(string.Format("{0}:\t{1}", item.Key, item.Value));



            return sb.ToString();

        }

    }


    

更比较通用的是ExpandoObject类,可以在运行是动态的添加或删除成员,包括属性,事件等等。

例如,

1. 添加新属性:

dynamic sampleObject = new ExpandoObject();

sampleObject.test = "Dynamic Property" ;
Console.WriteLine(sampleObject.test);
Console.WriteLine(sampleObject.test.GetType());

2. 添加事件(lambda或event):

// 2.1 add lambda expression

sampleObject.number = 10;
sampleObject.Increment = (Action)(() => { sampleObject.number++; });
// Before calling the Increment method.
Console.WriteLine(sampleObject.number);
sampleObject.Increment();
// After calling the Increment method.
Console.WriteLine(sampleObject.number);

// 2.2 add event

// Create a new event and initialize it with null. must declare it first.
sampleObject.sampleEvent = null ;

  // Add an event handler.
sampleObject.sampleEvent += new EventHandler(SampleHandler);
  // Raise an event for testing purposes.
sampleObject.sampleEvent(sampleObject, new EventArgs());
  // Event handler.
  static void SampleHandler( object sender, EventArgs e)
 {
        Console.WriteLine( "SampleHandler for {0} event" , sender);
}

3. 作为参数传递

dynamic employee = new ExpandoObject();
employee.Name = "John Smith" ;
employee.Age = 33;

WritePerson(employee);

private static void WritePerson( dynamic person)

{
        Console.WriteLine( "{0} is {1} years old." , person.Name, person.Age);

}
4. 枚举成员:

因为该类实现了IDictionary<string, object>接口,所以可以枚举它的成员。

dynamic employee = new ExpandoObject();
employee.Name = "John Smith";
employee.Age = 33;

foreach (var property in (IDictionary<String, Object>)employee)
{
    Console.WriteLine(property.Key + ": " + property.Value);
}
// This code example produces the following output:
// Name: John Smith
// Age: 33

// 删除某个成员
dynamic employee = new ExpandoObject();
employee.Name = "John Smith";
((IDictionary<String, Object>)employee).Remove("Name");

5. 通知机制
// Add "using System.ComponentModel;" line
// to the beginning of the file.
class Program
{
    static void Test()
    {
        dynamic employee = new ExpandoObject();
        ((INotifyPropertyChanged)employee).PropertyChanged +=
            new PropertyChangedEventHandler(HandlePropertyChanges);
        employee.Name = "John Smith";
    }

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

 

 

Implementing Dynamic Interfaces


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论