欢迎来到 IT实训基地-南通科迅教育
咨询电话:0513-81107100
常用算法帖(C#): Dynamic Object
2017/1/14
科迅教育
348
南通Web前端培训学校

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

Var类型和Dynamic类型
[csharp] 
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!
     */ 

        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
[csharp]
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!
 
             */ 
        } 

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类)
[csharp]
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; 
       } 
   } 

 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;
        }
    }
[csharp]
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
 
     */ 

        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

             */
        }

77
关闭
先学习,后交费申请表
每期5位名额
在线咨询
免费电话
QQ联系
先学习,后交费
TOP
您好,您想咨询哪门课程呢?
关于我们
机构简介
官方资讯
地理位置
联系我们
0513-91107100
周一至周六     8:30-21:00
微信扫我送教程
手机端访问
南通科迅教育信息咨询有限公司     苏ICP备15009282号     联系地址:江苏省南通市人民中路23-6号新亚大厦三楼             法律顾问:江苏瑞慈律师事务所     Copyright 2008-