Net反射效率(转载)

 
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Reflection;
 

namespace TestAssembly
{
    public class TestClass : ITestInterface
    {
        public TestClass() { }
        public double TestMethod(double param)
        {
            return param * 0.75;
        }
    }
 
    public interface ITestInterface
    {
        double TestMethod(double param);
    }
 
    class Program
    {
        public static void Main(string[] args)
        {
            int n = 1000;
            Test1(n);//直接调用
            Test2(n);//通过InvokeMember调用
            Test3(n);//通过接口调用
            Test4(n);//绑定至delegate
            Console.In.ReadLine();
        }
 

        public delegate double TestDelegate(double param);
 
        //首先,写代码测量直接运行的效率,代码如下:
        static void Test1(int n)
        {
            TestClass tc = new TestClass();
            DateTime startTime = DateTime.Now;
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    tc.TestMethod(1.0);
            TimeSpan ts = DateTime.Now - startTime;
            Console.Out.WriteLine("Test1: " + ts);
        }
 
        //接着是通过InvokeMember调用,代码如下:
        static void Test2(int n)
        {
            Type testType = typeof(TestClass);
            object obj = testType.InvokeMember(null,BindingFlags.CreateInstance, null, null, null);
            DateTime startTime = DateTime.Now;
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    testType.InvokeMember("TestMethod", BindingFlags.InvokeMethod, null, obj, new object[] { 1.0 });
            TimeSpan ts = DateTime.Now - startTime;
            Console.Out.WriteLine("Test2: " + ts);
        }
 
        //然后,是将获得的object用接口来引用,然后调用方法,代码如下:
        static void Test3(int n)
        {
            Type testType = typeof(TestClass);
 
            object obj = testType.InvokeMember(null,BindingFlags.CreateInstance, null, null, null);
 
            ITestInterface instance = (ITestInterface)obj;
 
            DateTime startTime = DateTime.Now;
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    instance.TestMethod(1.0);
            TimeSpan ts = DateTime.Now - startTime;
            Console.Out.WriteLine("Test3: " + ts);
        }
 

        static void Test4(int n)
        {
            Type testType = typeof(TestClass);
            object obj = testType.InvokeMember(null,BindingFlags.CreateInstance, null, null, null);
 
            TestDelegate testMethod = (TestDelegate)Delegate.CreateDelegate(typeof(TestDelegate), obj, "TestMethod");
 
            DateTime startTime = DateTime.Now;
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    testMethod(1.0);
            TimeSpan ts = DateTime.Now - startTime;
            Console.Out.WriteLine("Test4: " + ts);
        }
    }
}
 
Test1,Test2,Test3,Test4比较效果。

转载于:https://www.cnblogs.com/chinaagan/archive/2010/09/13/reflector.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值