c#高效反射调用方法类

c#高效反射调用方法类

ID:14168985

大小:39.50 KB

页数:13页

时间:2018-07-26

c#高效反射调用方法类_第1页
c#高效反射调用方法类_第2页
c#高效反射调用方法类_第3页
c#高效反射调用方法类_第4页
c#高效反射调用方法类_第5页
资源描述:

《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

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。