zoukankan      html  css  js  c++  java
  • Implementing Dynamic Interfaces

    名词解释

    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);
        }
    }

  • 相关阅读:
    [Java算法分析与设计]--链式堆栈的设计
    Spring 单例模式和多例模式
    lucene_09_solrj的使用
    lucene_08_solr导入数据库数据
    lucene_07_solr使用IK分词器
    lucene_06_solr域
    lucene_05_solr配置
    lucene_04_解析语法查询
    lucene_03_索引的增删改查
    lucene_02_IKAnalyre
  • 原文地址:https://www.cnblogs.com/bear831204/p/2304863.html
Copyright © 2011-2022 走看看