欢迎来到天天文库
浏览记录
ID:14168985
大小:39.50 KB
页数:13页
时间:2018-07-26
《c#高效反射调用方法类》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、C#高效反射调用方法类Asp.Net和php中的md5C#中const与readonly的用法和区别C#高效反射调用方法类C#2010-10-2814:53:12阅读62评论0字号:大中小订阅MethodInfomethodInfo=typeof(Program).GetMethod("Call");methodInfo.Invoke(program,parameters);methodInfo实际上已经用到反射了,只不过此时的反射相比较于后边的Invoke方法性能损失很小,可以忽略,影
2、响性能部分在Invoke而不在一次简单的GetMethod,MethodInfo可以缓存,因此只需要取一次就够了,所以完全可以忽略不计。方法一:不需要用emit和lambda只要用Deleaget就能达到高效率:1.privatedelegatevoidmyDelegate(stringstr);privatestaticmyDelegateexecPage=null;publicvoidPage_Load(){if(execPage==null){//AppUtilityapp=newAppUtility
3、();//execPage=(myDelegate)Delegate.CreateDelegate(typeof(myDelegate),app,"ExecutePage");//执行实体类的方法execPage=(myDelegate)Delegate.CreateDelegate(typeof(myDelegate),typeof(AppUtility),"ExecutePage");//静态类方法}execPage("TP_Default.aspx");}2.Funcf=Delegate.Create
4、Delegate(typeof(Func),MethodInfo);f(...);http://blog.zhaojie.me/2008/11/invoke-method-by-lambda-expression.html方法二:使用传统的.net反射机制,调用类的方法时,在调用频率大的情况下,会感觉速度很慢。最近浏览卢彦的博客时,找到一个他改进后的反射调用类。试用以后感觉效率明显提高,特推荐给大家。作者重新实现了,反射调用方法,但是调用接口和.net原有方法一致。而且调用时抛出的异常为所调用类的实际异常,
5、不像传统方式返回为包装异常。文章来源:http://www.codeproject.com/csharp/FastMethodInvoker.asp快速反射调用类usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Reflection;usingSystem.Reflection.Emit;namespaceFastMethodInvoker{classFastInvoke{publicdelegateobject
6、FastInvokeHandler(objecttarget,object[]paramters);staticobjectInvokeMethod(FastInvokeHandlerinvoke,objecttarget,paramsobject[]paramters){returninvoke(null,paramters);}publicstaticFastInvokeHandlerGetMethodInvoker(MethodInfomethodInfo){DynamicMethoddynamicM
7、ethod=newDynamicMethod(string.Empty,typeof(object),newType[]{typeof(object),typeof(object[])},methodInfo.DeclaringType.Module);ILGeneratoril=dynamicMethod.GetILGenerator();ParameterInfo[]ps=methodInfo.GetParameters();Type[]paramTypes=newType[ps.Length];for
8、(inti=0;i
此文档下载收益归作者所有