• dynamic 动态获取object数据


    1.替代XXX.GetType().GetProperty("YYY").GetValue(XXX)

    static object GetPerson()
            {
                return new Person { Name = "Leo" };
            }    
    

     有时候难免会遇到这种返回object的倒霉代码(特别是跟反射有关的时候),这时我们又要访问其中的某个属性,那个费劲啊,现在有了dynamic感觉好多l 

    object objPerson = GetPerson();
                var objName =  objPerson.GetType().GetProperty("Name").GetValue(objPerson);
                Console.WriteLine(objName);
    
                dynamic dynPerson = GetPerson();
                var dynName = dynPerson.Name;
                Console.WriteLine(dynName);

    另一个好处是性能会得到一程度的提升:

    Watch = new Stopwatch();
                Watch.Start();
                for (int i = 0; i < 1000000; i++)
                {
                    objName = objPerson.GetType().GetProperty("Name").GetValue(objPerson);
                }
                Watch.Stop();
                Console.WriteLine(Watch.Elapsed);
    
    
                Watch.Restart();
                for (int i = 0; i < 1000000; i++)
                {
                    dynName = dynPerson.Name;
                }
                Watch.Stop();
                Console.WriteLine(Watch.Elapsed);

    2.拯救接手接口没设计好的代码的倒霉孩子

      比如这里有N个WCF服务,返回了N个对象的集合,这几个对象没啥关系,其实又有一点关系,倒霉孩子又不会让Entity Framework生成的类自动继承某个接口(本文里用本地方法代替WCF服务)。

            这里来举一个例子,首先有下面2个倒霉的类,同样string类型的name是可以提取接口的(这里真的合适提取么……),同样名称但不同类型的ID,完全无关的Age和Price。

    public class Person
        {
            public int ID { get; set; }
    
            public string Name { get; set; }
    
            public int Age { get; set; }
    
            public static List<Person> GetPersonList()
            {
                return new List<Person>
                {
                    new Person{ Name = "Leo1" , Age = 10 },
                    new Person{ Name = "Leo2" , Age = 20 },
                    new Person{ Name = "Leo3" , Age= 30 }
                };
            }
        }
    
        public class Car
        {
            public Guid ID { get; set; }
    
            public string Name { get; set; }
    
            public double Price { get; set; }
    
            public static List<Car> GetCarList()
            {
                return new List<Car>
                {
                    new Car{ Name = "Focus1" , Price = 100 },
                    new Car{ Name = "Focus2" , Price = 200 },
                    new Car{ Name = "Focus3" , Price = 300 }
                };
            }
        }

      我用2个static方法返回不同类型的List<T>来模拟WCF中最普通的调用。

    static void Main(string[] args)
            {
                List<dynamic> list = new List<dynamic>();
                //用本地方法替代WCF服务,您假装是通过WCF获取的list
                Person.GetPersonList().ForEach((p) => list.Add(p));
                TestDynamic2(list,"Leo2");
    
                list = new List<dynamic>();
                //用本地方法替代WCF服务,您假装是通过WCF获取的list
                Car.GetCarList().ForEach((c) => list.Add(c));
                TestDynamic2(list,"Focus3");
    
                Console.ReadKey();
            }
    
            private static void TestDynamic2(List<dynamic> list,string name)
            {
                //可以无差别的使用ID和Name属性
                dynamic first = list.OrderBy(d => d.ID).FirstOrDefault(d => d.Name.Contains(name));
    
                //差别对待不同的属性,这里供参考,不建议这么写,这会导致依赖具体的类型
                if (first is Person)
                {
                    Console.WriteLine(first.Age);
                }
                else
                {
                    Console.WriteLine(first.Price);
                }
            }

    from

    http://www.cnblogs.com/manupstairs/p/3166850.html

  • 相关阅读:
    动手学习TCP:TCP连接建立与终止
    动手学习TCP: 环境搭建
    Python 数据分析4
    Django unittest 单元测试
    Django commands自定制
    Python mac安装mysqlclient的一个bug
    Centos7 开机启动流程
    Centos 06 文件类型和扩展名&索引节点inode和存储块block
    Centos 05 系统目录讲解
    Linux 踩过的坑系列-01
  • 原文地址:https://www.cnblogs.com/yexiaoyanzi/p/4648358.html
Copyright © 2020-2023  润新知