dynamic的一些使用心得

        dynamic关键字才出来的时候,觉得真是没什么用,谁总是和com交互来交互去啊,唯恐避之不及啊。

        后来逐渐算是有了一些使用心得,发现这货还真是犀利啊,故在此举几个例子,起抛砖引玉之用。

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

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

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

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

       本文提供了使用dynamic的一点心得,如果有不对的对方,还望各位不吝指出,非常感谢!

posted @ 2013-07-02 14:05  楼上那个蜀黍  阅读(9678)  评论(7编辑  收藏  举报