你好,游客 登录 注册 搜索
背景:
阅读新闻

常用算法帖(C#): Dynamic Object - GhostHouse

[日期:2013-04-15] 来源:  作者: [字体: ]

    Dynamic Object包含在.Net4.0中,该对象可以允许我们在运行时中改变它的类型,有了这种万金油还有什么问题是我们不能解决的呢?我们来看看它的几个使用例子吧:

Var类型和Dynamic类型

        public static void TestDynamicObject()
        { 
            //var object and dynamic object
            var varObj = "this is a var object";
            dynamic dyObj = "this is a dynamic object";

            Console.WriteLine(varObj);
            Console.WriteLine(dyObj);

            dyObj = new string[] { dyObj, "I also can do it liking this!" };
            Console.WriteLine(dyObj[0]);
            Console.WriteLine(dyObj[1]);

            /*OUT PUT
                this is a var object
                this is a dynamic object
                this is a dynamic object
                I also can do it liking this!
             */
        }


ExpandObject

public static void TestExpandObject()
        {
            dynamic data = new ExpandoObject();

            data.FirstAttribute = "FirstAttribute";
            data.SecondAttribute = "SecondAttribute";
            data.ThirdAttribute = "ThirdAttribute";

            ArrayList nums = new ArrayList();
            for (int i = 1; i <= 10; i++)
            {
                nums.Add(i);
            }

            data.Nums = nums;

            foreach (int tmp in data.Nums)
            {
                Console.WriteLine(tmp);
            }

            Console.WriteLine(data.FirstAttribute);
            Console.WriteLine(data.SecondAttribute);
            Console.WriteLine(data.ThirdAttribute);

            Action GB = () => { Console.WriteLine("This is dynamic method."); };
            data.MyMethod2 = GB;
            data.MyMethod2();

            Action<string> GhostBear = a => Console.WriteLine(a);
            data.MyMethod = GhostBear;
            data.MyMethod("Go!Go!Fighting!");

            /*OUT PUT
                1
                2
                3
                4
                5
                6
                7
                8
                9
                10
                FirstAttribute
                SecondAttribute
                ThirdAttribute
                This is dynamic method.
                Go!Go!Fighting!

             */
        }

 

自定义扩展的Dynamic对象(需要继承DynamicObject类)

 class MyDynamicObject : DynamicObject
    {
        Dictionary<string, object> data = new Dictionary<string, object>();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="binder">传进来的参数,包含了调用信息</param>
        /// <param name="result">返回包含的对象</param>
        /// <returns>返回是否成功</returns>
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            bool isContain = false;
            if (data.ContainsKey(binder.Name))
            {
                result = data[binder.Name];
                isContain = true;
            }
            else
            {
                result = "Not Find";
            }
            return isContain;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="binder">传进来的参数,包含了调用信息</param>
        /// <param name="value">传进来的参数,包含了调用信息</param>
        /// <returns></returns>
        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            bool result = false;
            if (!data.ContainsKey(binder.Name))
            {
                data[binder.Name] = value;
                result = true;

            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="binder">传进来的参数,包含了调用信息</param>
        /// <param name="args">传进来的参数,包含了调用信息</param>
        /// <param name="result">方法体执行后返回的结果</param>
        /// <returns>调用是否成功</returns>
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            dynamic tmp = data[binder.Name];
            result = null;

            tmp((string)args[0]);

            return true;
        }
    }
        public static void TestExtendDynamicObject()
        {
            dynamic myDynamicObject = new MyDynamicObject();
            myDynamicObject.FirstAttribute = "FirstAttribute";

            Action<string> myMethod = a => Console.WriteLine(a);
            myDynamicObject.myMethod = myMethod;

            Console.WriteLine(myDynamicObject.FirstAttribute);
            myDynamicObject.myMethod("this is Dynamic Object");

            /*OUT PUT
             FirstAttribute
             this is Dynamic Object

             */
        }


 

 





收藏 推荐 打印 | 录入:admin | 阅读:
相关新闻