赞
踩
MethodInfo methodInfo = typeof(Program).GetMethod("Call");
methodInfo.Invoke(program, parameters);
methodInfo实际上已经用到反射了,只不过此时的反射相比较于后边的Invoke方法性能损失很小,可以忽略,影响性能部分在Invoke而不在一次简单的GetMethod,MethodInfo可以缓存,因此只需要取一次就够了,所以完全可以忽略不计。
方法一:
不需要用emit和lambda只要用Deleaget就能达到高效率:
1.
private delegate void myDelegate(string str);
private static myDelegate execPage = null;
public void Page_Load()
{
if (execPage == null)
{
//AppUtility app = new AppUtility();
//execPage = (myDelegate)Delegate.CreateDelegate(typeof(myDelegate), app, "ExecutePage"); //执行实体类的方法
execPage = (myDelegate)Delegate.CreateDelegate(typeof(myDelegate), typeof(AppUtility), "ExecutePage"); //静态类方法
}
execPage("TP_Default.aspx");
}
2.
Func f = Delegate.CreateDelegate(typeof(Func), MethodInfo);
f(...);
http://blog.zhaojie.me/2008/11/invoke-method-by-lambda-expression.html
方法二:
使用传统的.net反射机制,调用类的方法时,在调用频率大的情况下,会感觉速度很慢。最近浏览卢彦的博客时,找到一个他改进后的反射调用类。试用以后感觉效率明显提高,特推荐给大家。作者重新实现了,反射调用方法,但是调用接口和.net原有方法一致。而且调用时抛出的异常为所调用类的实际异常,不像传统方式返回为包装异常。
文章来源:http://www.codeproject.com/csharp/FastMethodInvoker.asp
快速反射调用类
效果测试程序
http://www.cnblogs.com/heekui/archive/2007/01/10/616654.html
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。