相关疑难解决方法(0)

加速Reflection Invoke C#/ .NET

有很多关于加速反射调用的帖子,例如:

使用.NET/C中的委托加速Reflection API

https://codeblog.jonskeet.uk/2008/08/09/making-reflection-fly-and-exploring-delegates/

和这里:

示例:使用.NET/C#中的委托加速Reflection API



我的问题是关于加速泛型调用.这有可能吗?

我有一个抽象类和一个实现它的类......

public abstract class EncasulatedMessageHandler<T> where T : Message
{
    public abstract void HandleMessage(T message);
}

public class Handler : EncasulatedMessageHandler<MyMessageType>
{
    public int blat = 0;
    public override void HandleMessage(MyMessageType message) { blat++; }
}
Run Code Online (Sandbox Code Playgroud)

我想要做的是建立这些消息处理程序类的列表并快速调用它们的HandleMessage()


目前,我正在做的事情大致如下:

object handler = Activator.CreateInstance(typeof(Handler)); // Ignore this, this is done up front.

MethodInfo method = type.GetMethod("HandleMessage", BindingFlags.Instance | BindingFlags.Public);

Action<object> hook = new Action<object>(delegate(object message)
{
    method.Invoke(handler, new object[] { message });
});

// …
Run Code Online (Sandbox Code Playgroud)

.net c# reflection invoke

15
推荐指数
3
解决办法
1万
查看次数

使用.NET/C中的委托加速Reflection API

这篇文章有评论if you need to call the method multiple times, use reflection once to find it, then assign it to a delegate, and then call the delegate..

  • 如何以及为何delegate更快地运作?谁能有一些例子吗?
  • 我可以打电话给你caching吗?如果是这样,除了使用委托的缓存方法之外还有其他方法吗?

添加

我想出了一个delegate 在这里使用的例子.

.net c# reflection delegates

7
推荐指数
1
解决办法
3392
查看次数

标签 统计

.net ×2

c# ×2

reflection ×2

delegates ×1

invoke ×1