示例:加快反射API与委托的.NET / C# [英] Example : Speeding up Reflection API with delegate in .NET/C#
问题描述
由于被要求在此<一个href="http://stackoverflow.com/questions/6430479/speeding-up-reflection-api-with-delegate-in-net-c/6430561#6430561">post,我想出了一个使用委派用来加快恢复体力的.NET / C#的例子。
As is asked in this post, I came up with an example that uses Delegate to speedup Refection in .NET/C#.
不过,我运行的时候得到这个错误(编译正常工作)。可能是什么问题?
However, I got this error when running (compilation works fine). What might be wrong?
Unhandled Exception: System.ArgumentException: type is not a subclass of Multicastdelegate
at System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method, Boolean throwOnBindFailure, Boolean allowClosed) [0x00000] in <filename unknown>:0
at System.Delegate.CreateDelegate (System.Type type, System.Reflection.MethodInfo method, Boolean throwOnBindFailure) [0x00000] in <filename unknown>:0
at System.Delegate.CreateDelegate (System.Type type, System.Reflection.MethodInfo method) [0x00000] in <filename unknown>:0
at EX.RefTest.DelegateTest () [0x00000] in <filename unknown>:0
at EX.RefTest.Main () [0x00000] in <filename unknown>:0
ADDED
这是(工作)来源$ C $ C得益于乔恩和放大器的帮助; ChaosPandion。
ADDED
This is the (working) source code thanks to the help from Jon & ChaosPandion.
using System.Reflection;
using System;
namespace EX
{
public class Hello
{
// properties
public int Valx {get; set;}
public int Valy {get; set;}
public Hello()
{
Valx = 10; Valy = 20;
}
public int Sum(int x, int y)
{
Valx = x; Valy = y;
return (Valx + Valy);
}
}
public class RefTest
{
static void DelegateTest()
{
Hello h = new Hello();
Type type = h.GetType();
MethodInfo m = type.GetMethod("Sum");
// Wrong! Delegate call = Delegate.CreateDelegate(type, m);
Delegate call = Delegate.CreateDelegate(typeof(Func<int, int, int>), h, m);
int res = (int) call.DynamicInvoke(new object[] {100, 200});
Console.WriteLine("{0}", res);
// This is a direct method implementation from Jon's post, and this is much faster
Func<int, int, int> sum = (Func<int, int, int>) Delegate.CreateDelegate(typeof(Func<int, int, int>), h, m);
res = sum(100, 200);
Console.WriteLine("{0}", res);
}
static void Main()
{
DelegateTest();
}
}
}
ADDED2
基于乔恩的答案,我做了一些性能测试使用的总和1000倍。
相较于使用的方法(INT)call.DynamicInvoke(新对象[] {100,200});
, Func键&LT; INT,INT, INT&GT;总和=(Func键&LT; INT,INT,INT&GT;)Delegate.CreateDelegate(typeof运算(Func键&LT; INT,INT,INT&GT;),H,M);
快300倍。
ADDED2
Based on Jon's answer, I did some performance test to use sum 1000 time.
Compared to the method of using (int) call.DynamicInvoke(new object[] {100, 200});
, Func<int, int, int> sum = (Func<int, int, int>) Delegate.CreateDelegate(typeof(Func<int, int, int>), h, m);
is 300 times faster.
推荐答案
您好
不是委托类型 - 所以你不能将它传递到 Delegate.CreateDelegate
作为第一个参数。您需要使用相同的参数类型和返回类型的委托类型 - 在这种情况下,是这样的:
Hello
isn't a delegate type - so you can't pass it into Delegate.CreateDelegate
as the first argument. You need a delegate type with the same parameter types and return type - in this case, something like:
delegate int Foo(int x, int y);
或
Func<int, int, int>
另外请注意, Delegate.CreateDelegate
您已经叫过载,就是要用于的静态的方法 - 你应该利用过载也采取了委托的目标(在这种情况下, ^ h
)。
Also note that the overload of Delegate.CreateDelegate
you've called is meant to be used for a static method - you ought to be using the overload which also takes the target of the delegate (in this case, h
).
我有一个博客文章这说明使用 Delegate.CreateDelegate
加快访问。请注意,我不希望 DynamicInvoke
比直接用反射调用方法显著快......它仍然将不得不检查等,真的是你想要的参数类型一个叫做静态强类型委托类型(而不是动态),以使事情变得真快。
I have a blog post which shows using Delegate.CreateDelegate
for speeding up access. Note that I wouldn't expect DynamicInvoke
to be significantly faster than calling the method directly with reflection... it's still going to have to check the parameter types etc. Really you want a strongly-typed delegate type called statically (as opposed to dynamically) to make things really fast.
这篇关于示例:加快反射API与委托的.NET / C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!