c#中DynamicObject类型的测试

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

public class MyExpandoObject : DynamicObject
{
    private Dictionary<string, object> _dict = new Dictionary<string, object>();

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        result = null;
        if (_dict.ContainsKey(binder.Name.ToUpper()))
        {
            result = _dict[binder.Name.ToUpper()];
            return true;
        }
        return false;
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        if (_dict.ContainsKey(binder.Name.ToUpper()))
            _dict[binder.Name.ToUpper()] = value;
        else
            _dict.Add(binder.Name.ToUpper(), value);
        return true;
    }

    public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
    {
        result = new Action(() => { });

        if (_dict.ContainsKey(binder.Name.ToUpper()))
        {
            Type dictType = _dict.GetType();
            try
            {
                result = dictType.InvokeMember(
                            binder.Name,
                            System.Reflection.BindingFlags.InvokeMethod,
                            null,
                            this,
                            args
                        );
                return true;
            }
            catch
            {
                return false;
            }
        }
        return false;
    }

    //public static void aa(string str)
    public void aa(string str)
    {
        Console.WriteLine("input {0}", str);
    }

    //public static void bb()
    public void bb()
    {
        Console.WriteLine("i loving coding!!!");
    }
    public static void ComeFrom(string country)
    {
        Console.WriteLine("I'm from {0}", country);
    }
}

class TestMyExpandoObject
{
    class gg : DynamicObject
    {
        public static void TestComeFrom(string country)
        {
            Console.WriteLine("I'm from {0}", country);
        }

        Dictionary<string, object> dictionary = new Dictionary<string, object>();
        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            dictionary[binder.Name] = value;
            return true;
        }
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            return dictionary.TryGetValue(binder.Name, out result);
        }
    }

    static void Main()
    {
        dynamic t = new gg();
        t.fn = new Action<string>(s => gg.TestComeFrom(s));
        t.fn("t");
        Console.WriteLine("========================================");

        dynamic vessel = new MyExpandoObject();
        vessel.Name = "Little Miss Understood";
        vessel.Age = 12;
        vessel.KeelLengthInFeet = 32;
        vessel.Longitude = 37.55f;
        vessel.Latitude = -76.34f;

        //vessel.aa("China");
        //vessel.bb();
        //Console.WriteLine("========================================");

        vessel.fn = new Action<string>(s => MyExpandoObject.ComeFrom(s));
        vessel.fn("t");
        Console.WriteLine("========================================");
        //vessel.fb = new Action(MyExpandoObject.bb);

        Console.WriteLine("The {0} year old vessel " +
                          "named {1} has a keel length of {2} feet " +
                          "and is currently located at {3} / {4}.",
                          vessel.AGE, vessel.name,
                          vessel.keelLengthINfeet,
                          vessel.Longitude, vessel.Latitude);

        Console.ReadLine();
    }
}

实际的时候,建议用ConcurrentDictionary替换Dictionary,eg:

class Person : System.Dynamic.DynamicObject
        {
            //Dictionary<string, object> properties = new Dictionary<string, object>();
            ConcurrentDictionary<string, object> properties = new ConcurrentDictionary<string, object>();

            public override bool TryGetMember(System.Dynamic.GetMemberBinder binder, out object result)
            {
                return properties.TryGetValue(binder.Name, out result);
            }

            public override bool TrySetMember(System.Dynamic.SetMemberBinder binder, object value)
            {
                properties[binder.Name] = value;
                return true;
            }

            public ConcurrentDictionary<string, object> GetProperties()
            {
                return properties;
            }

            public override string ToString()
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("--- Person attributes ---");

                foreach (var key in properties.Keys)
                {
                    //We use the chaining property of the StringBuilder methods
                    sb.Append(key).Append(": ").AppendLine(properties[key].ToString());
                }

                return sb.ToString();
            }
        }

        static void Main(string[] args)
        {
            dynamic person = new Person();
            person.Name = "Florian";
            person.Age = 28;
            Console.WriteLine(person);

            person.Country = "Germany";
            Console.WriteLine(person);

            Console.ReadKey();
        }



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值