Asp.Net和php中的md5
C#中const与readonly的用法和区别
C#高效反射调用方法类
C# 2010-10-28 14:53:12 阅读62 评论0
字号:大中小
订阅
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(...);
/2008/11/invoke-method-by-lambda-expression.html
方法二:
使用传统的反射机制,调用类的方法时,在调用频率大的情况下,会感觉速度很慢。最近浏览卢彦的博客时,到一个他改进后的反射调用类。试用以后感觉效率明显提高,特推荐给大家。作者重新
实现了,反射调用方法,但是调用接口和原有方法一致。而且调用时抛出的异常为所调用类的实际异常,不像传统方式返回为包装异常。
文章来源:deproject/csharp/FastMethodInvoker.asp
快速反射调用类
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;
namespace FastMethodInvoker
{
class FastInvoke
{
public delegate object FastInvokeHandler(object target, object[] paramters);
static object InvokeMethod(FastInvokeHandler invoke, object target, params object[] paramters)
{
return invoke(null, paramters);
}
public static FastInvokeHandler GetMethodInvoker(MethodInfo methodInfo)
{
DynamicMethod dynamicMethod = new DynamicMethod(string.Empty, typeof(object), new Type[] { typeof(object), typeof(object[]) }, methodInfo.DeclaringType.Module);
ILGenerator il = dynamicMethod.GetILGenerator();
ParameterInfo[] ps = methodInfo.GetParameters();
Type[] paramTypes = new Type[ps.Length];
for (int i = 0; i < paramTypes.Length; i++)
{
if (ps[i].ParameterType.IsByRef)
paramTypes[i] = p
s[i].ParameterType.GetElementType();
else
paramTypes[i] = ps[i].ParameterType;
}
LocalBuilder[] locals = new LocalBuilder[paramTypes.Length];
for (int i = 0; i < paramTypes.Length; i++)
{
locals[i] = il.DeclareLocal(paramTypes[i], true);
}
for (int i = 0; i < paramTypes.Length; i++)
{
il.Emit(OpCodes.Ldarg_1);
EmitFastInt(il, i);
il.Emit(OpCodes.Ldelem_Ref);
EmitCastToReference(il, paramTypes[i]);
il.Emit(OpCodes.Stloc, locals[i]);
}
if (!methodInfo.IsStatic)
{
il.Emit(OpCodes.Ldarg_0);
}
for (int i = 0; i < paramTypes.Length; i++)
{
if (ps[i].ParameterType.IsByRef)
il.Emit(OpCodes.Ldloca_S, locals[i]);
else
il.Emit(OpCodes.Ldloc, locals[i]);
}
if (methodInfo.IsStatic)
il.EmitCall(OpCodes.Call, methodInfo, null);
else
il.EmitCall(OpCodes.Callvirt, methodInfo, null);
if (methodInfo.ReturnType == typeof(void))
il.Emit(OpCodes.Ldnull);
else
EmitBoxIfNeeded(il, methodInfo.ReturnType);
for (int i = 0; i < paramTypes.Length; i++)
{
if (ps[i].ParameterType.IsByRef)
{
il.Emit(OpCodes.Ldarg_1);
EmitFastInt(il, i);
il.Emit(OpCodes.Ldloc, locals[i]);
if (locals[i].LocalType.IsValueType)
il.Emit(OpCodes.Box, locals[i].LocalType);
il.Emit(OpCodes.Stelem_Ref);
}
}
il.Emit(OpCodes.Ret);
FastInvokeHandler invoder = (FastInvokeHandler)dynamicMethod.CreateDelegate(typeof(FastInvokeHandler));
return invoder;
}
private static void EmitCastToReference(ILGenerator il, System.Type type)
{
if (type.IsValueType)
{
il.Emit(OpCodes.Unbox_Any, type);
typeof的用法}
else
{
il.Emit(OpCodes.Castclass, type);
}
}
private static void EmitBoxIfNeeded(ILGenerator il, System.Type type)
{
if (type.IsValueType)
{
il.Emit(OpCodes.Box, type);
}
}
private static void EmitFastInt(ILGenerator il, int value)
{
switch (value)
{
case -1:
il.Emit(OpCodes.Ldc_I4_M1);
return;
case 0:
il.Emit(OpCodes.Ldc_I4_0);
return;
case 1:
il.Emit(OpCodes.Ldc_I4_1);
return;
case 2:
il.Emit(OpCodes.Ldc_I4_2);
return;
case 3:
il.Emit(OpCodes.Ldc_I4_3);
return;
case 4:
il.Emit(OpCodes.Ldc_I4_4);
return;
case 5:
il.Emit(OpCodes.Ldc_I4_5);
return;
case 6:
il.Emit(OpCodes.Ldc_I4_6);
return;
case 7:
il.Emit(OpCodes.Ldc_I4_7);
return;
case 8:
il.Emit(OpCodes.Ldc_I4_8);
return;
}
if (value > -129 && value < 128)
{
il.Emit(OpCodes.Ldc_I4_S, (SByte)value);
}
else
{
il.Emit(OpCodes.Ldc_I4, value);
}
}
}
}
效果测试程序
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace FastMethodInvoker
{
class Program
{
static void Main(string[] args)
{
Type t = typeof(Person);
MethodInfo methodInfo = t.GetMethod("Say");
Person person = new Person();
string word = "hello";
Person p = null;
object[] param = new object[] { word, p, 3 };
int TestTimes = 100000; //测试次数,可自行调节看效果
#region 传统方式反射
try
{
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < TestTimes; i++)
{
methodInfo.Invoke(person, param);
}
watch.Stop();
Console.WriteLine(TestTimes.ToString() + " times invoked by Reflection: " + watch.ElapsedMilliseconds + "ms");
}
catch (System.Exception ex)
{
Console.WriteLine("传统方式反射 直接错误:" + ex.Message);
Console.WriteLine("传统方式反射 内部错误:" + ex.InnerException.Message);
}
#endregion
#region 快速反射
try
{
Stopwatch watch1 = new Stopwatch();
FastInvoke.FastInvokeHandler fastInvoker = FastInvoke.GetMethodInvoker(methodInfo);
watch1.Start();
for (int i = 0; i < TestTimes; i++)
{
fastInvoker(person, param);
}
watch1.Stop();
Console.WriteLine(TestTimes.ToString() + " times invoked by FastInvoke: " + watch1.Elapsed
Milliseconds + "ms");
}
catch (System.Exception ex)
{
Console.WriteLine("快速反射 错误:" + ex.Message);
}
#endregion
#region 直接调用
try
{
Stopwatch watch2 = new Stopwatch();
watch2.Start();
for (int i = 0; i < TestTimes; i++)
{
person.Say(ref word, out p, 3);
}
watch2.Stop();
Console.WriteLine(TestTimes.ToString() + " times invoked by DirectCall: " + watch2.ElapsedMilliseconds + "ms");
}
catch (System.Exception ex)
{
Console.WriteLine("直接调用 错误:" + ex.Message);
}
#endregion
Console.ReadLine();
}
}
public class Person
{
public void Say(ref string word, out Person p, int avi)
{
word = "ttt" + avi.ToString();
p = new Person();
//throw new System.Exception("出错了哦");
}
}
}
wwwblogs/heekui/archive/2007/01/10/616654.html
分享到:
阅读(62)| 评论(0)| 引用 (0) |举报
Asp.Net和php中的md5
C#中const与readonly的用法和区别
历史上的今天
多余字符BUG2008-10-28 15:45:38
高度自适应2008-10-28 14:42:18
浮动与流动混合布局应用2008-10-28 10:58:38
最近读者
登录后,您可以在此留下足迹。
sunzack
ㄨHCatㄨ
评论
点击登录|昵称:
公司简介 - 联系方法 - 招聘信息 - 客户服务 - 相关法律 - 博客风格 - 手机博客 - 订阅此博客
网易公司版权所有 ?1997-2011
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论