<span style="color:rgb(44,44,44); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; line-height:22px; background-color:rgb(46,48,51)"></span>
<p style="line-height:22px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> <span style="line-height:20px; font-size:10pt">MethodInfo methodInfo = typeof(Program).GetMethod(&quot;Call&quot;);<br style="line-height:20px"> methodInfo.Invoke(program, parameters);<br style="line-height:20px"> methodInfo实际上已经用到反射了,只不过此时的反射相比较于后边的Invoke方法性能损失很小,可以忽略,影响性能部分在<span style="line-height:20px">Invoke</span>而不在一次简单的GetMethod,MethodInfo可以缓存,因此只需要取一次就够了,所以完全可以忽略不计。</span></p>
<p style="line-height:22px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> <span style="line-height:20px; font-size:10pt"><span style="line-height:20px"></span></span> </p>
<p style="line-height:22px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> <span style="line-height:20px; font-size:10pt"><span style="line-height:20px">方法一:</span></span></p>
<span style="line-height:20px; font-size:10pt"></span>
<p style="line-height:20px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> 不需要用emit和lambda只要用Deleaget就能达到高效率:</p>
<p style="line-height:20px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> 1.</p>
<p style="line-height:20px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> private delegate void myDelegate(string str);</p>
<p style="line-height:20px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> private static myDelegate execPage = null;</p>
<p style="line-height:20px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> public void Page_Load()</p>
<p style="line-height:20px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> {<!-- --></p>
<p style="line-height:20px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> if (execPage == null)</p>
<p style="line-height:20px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> {<!-- --></p>
<p style="line-height:20px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> //AppUtility app = new AppUtility();</p>
<p style="line-height:20px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> //execPage = (myDelegate)Delegate.CreateDelegate(typeof(myDelegate), app, "ExecutePage"); //执行实体类的方法</p>
<p style="line-height:20px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> execPage = (myDelegate)Delegate.CreateDelegate(typeof(myDelegate), typeof(AppUtility), "ExecutePage"); //静态类方法</p>
<p style="line-height:20px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> }</p>
<p style="line-height:20px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> execPage("TP_Default.aspx"); </p>
<p style="line-height:20px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> }</p>
<p style="line-height:20px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> 2.<br style="line-height:20px"> Func f = Delegate.CreateDelegate(typeof(Func), MethodInfo); <br style="line-height:20px"> f(...);</p>
<p style="line-height:20px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> <a href="http://blog.zhaojie.me/2008/11/invoke-method-by-lambda-expression.html" style="color:rgb(44,44,44); line-height:20px; text-decoration:none">http://blog.zhaojie.me/2008/11/invoke-method-by-lambda-expression.html</a></p>
<p style="line-height:20px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> </p>
<p style="line-height:20px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> </p>
<p style="line-height:22px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> <span style="line-height:20px; font-size:10pt"><span style="line-height:20px">方法二:</span></span></p>
<p style="line-height:22px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> <span style="line-height:20px; font-size:10pt">使用传统的.net反射机制,调用类的方法时,在调用频率大的情况下,会感觉速度很慢。最近浏览</span><a href="http://www.agilelabs.cn/blogs/linkin/default.aspx" id="_ctl0__ctl0__ctl0__ctl0_BlogTitleHeader1__ctl0_BlogTitle" style="color:rgb(44,44,44); line-height:22px; text-decoration:none"><span style="line-height:20px; font-size:10pt">卢彦</span></a><span style="line-height:20px; font-size:10pt">的博客时,找到一个他改进后的反射调用类。试用以后感觉效率明显提高,特推荐给大家。作者重新实现了,反射调用方法,但是调用接口和.net原有方法一致。而且调用时抛出的异常为所调用类的实际异常,不像传统方式返回为包装异常。<br style="line-height:20px"> 文章来源:</span><a href="http://www.codeproject.com/csharp/FastMethodInvoker.asp" style="color:rgb(44,44,44); |
|