阅读背景:

使用dynamic动态设置属性值与反射设置属性值性能对比

来源:互联网 
static void Main(string[] args)

       {
           int times = 1000000;
 
           string value = "Dynamic VS  Reflection";
 
           //reflection 测试开始
           TestClass testTypeByReflection = new TestClass();
           Stopwatch watch1 = Stopwatch.StartNew();
           var property = typeof(TestClass).GetProperty("TestProperty");
           for (var i = 0; i < times; i++)
           {
               property.SetValue(testTypeByReflection, value, null);
           }
           Console.WriteLine(string.Format("Reflection耗时:{0} 毫秒", watch1.ElapsedMilliseconds));
 
 
           //dynamic 测试开始
           Stopwatch watch2 = Stopwatch.StartNew();
           dynamic testTypeByDynamic = new TestClass();
           for (int i = 0; i < times; i++)
           {
               testTypeByDynamic.TestProperty = value;
           }
           Console.WriteLine(string.Format("Dynamic耗时:{0} 毫秒", watch2.ElapsedMilliseconds));
 
           Console.ReadLine();
       }
static void Main(string[] args)

       {
 



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: