普通反射方法
MethodInfo methodInfo = typeof(Person).GetMethod("Say");
methodInfo.Invoke(new Person(), new object[]{"hello"});
快速反射方法
FastInvokeHandler fastInvoker = GetMethodInvoker(methodInfo);
fastInvoker(new Person(), new object[]{"hello"});
实现,首先需要定义一个委托:
public delegate object FastInvokeHandler(object target, object[] 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++)
{
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]);
}
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]);
}
il.Emit(OpCodes.Ldarg_0);
for (int i = 0; i < paramTypes.Length; i++)
{
il.Emit(OpCodes.Ldloc, locals[i]);
}
il.EmitCall(OpCodes.Call, methodInfo, null);
if (methodInfo.ReturnType == typeof(void))
il.Emit(OpCodes.Ldnull);
else
EmitBoxIfNeeded(il, methodInfo.ReturnType);
il.Emit(OpCodes.Ret);
FastInvokeHandler invoder =
(FastInvokeHandler)dynamicMethod.CreateDelegate(
typeof(FastInvokeHandler));
return invoder;
}
Conclusion
Well, I think this is a general way that can be used instead of most of the reflection methods to get about 50 times performance improvement. Any suggestions for improvements are welcome.
Extra advantage (reminded by MaxGuernsey): If an exception occurs in your code, FastInovker
would throw the original one, but the Method.Invoke
would throw a TargetInvocationException
.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
About the Author
Luyan
China
Member
|
I am currently working for a .NET framework names AgileFramework. It's introduction at here:
http://www.agilelabs.cn/agileframework
Now I'm living in China. I have been designing and developing .NET based software applications for 5+ years.
|
分享到:
相关推荐
在DOTNET 中的三种调用方式,动态快速放射
公司内部讲义,比较了SOA,RMI和Spring HttpInvoker。并介绍了Spring HttpInvoker的基本使用方法。
里面有两个工程,一个模拟服务端,一个模拟客户端。演示了最近本的httpinvoker的使用
http invoker 在开发完http接口后可以做http post测试
Hessian 、 HttpInvoker 、 XFire 、 Axis
NULL 博文链接:https://lggege.iteye.com/blog/369151
Spring httpInvoker使用标准java序列化机制,通过Http暴露业务服务。如果你的参数和返回值是比较复杂的,通过httpInvoker有巨大的优势。 1. 远程访问流程 1) 服务端定义服务接口 2) 服务端实现服务接口 3) 暴露服务...
Spring HttpInvoker,是一套基于Maven+Spring+SpringMVC+MyBatis框架,还包含了Invoker的客户端及服务器端的demo实例
Spring HTTP Invoker是spring框架中的一个远程调用模型,执行基于HTTP的远程调用,也就是说,可以通过防火墙,并使用java的序列化机制在网络间传递对象。客户端可以很轻松的像调用本地对象一样调用远程服务器上的...
模拟提交GET/POST,鞥呢返回详细头,cookies
springboot-httpinvoker-demo.zip
使用HttpInvoker,不需要额外的类库。和Hessian的轻量级传输协议不同的是,Spring HttpInvoker使用Java序列化来序列化参数和返回值,然后基于HTTP协议传输经序列化后的对象。当参数或返回值是复杂类型,并且不能通过...
maven-invoker-2.0.11.jar
自己仿dubbo Invoker实现,思想跟dubbo实现一致。。。。。。。。。。。
-urls 'file_name' : 带有 urls 列表的文本文件,其中包含一个按行的 RMI url。 -mbclass 'class_name' :托管 Bean JMX 对象名称。 -method 'method_name' :方法名称,由托管 Bean 接口公开。 [-params param1...
maven-invoker-plugin-1.5.jar
maven-invoker-plugin-1.3.jar
HTTP接口调用框架让 HTTP 接口调用跟调用本地方法一样自然优雅将 HTTP ...1.2.0版本后支持JDK6,之前的版本必须JDK8)技术栈动态代理反射注解自动包扫描快速开始一、添加maven依赖 <dependency> <groupId>com.github
用法首先,我们需要安装invoker gem来获取名为invoker命令行实用程序,我们可以通过以下方法进行操作: gem install invoker 当前,它仅适用于Ruby 2.0、2.1、2.2和2.3。手动的有关配置和使用Invoker的信息,请参见...
介绍了httpinvoker的简单使用方法,里面包含一个普通的方法,和一个实体类