[英]Pass an Action<T> to a generic method via reflection?
I know there's similar questions to this but I still wasn't able to resolve my issue. 我知道有类似的问题,但我仍然无法解决我的问题。
I have a EventManager
static class, which is just a wrapper for EventManagerInternal
, like so: 我有一个
EventManager
静态类,它只是EventManagerInternal
的包装器,如下所示:
public static class EventManager
{
public static void Subscribe<T>(Action<T> handler) where T : GameEvent
{
EventManagerInternal<T>.Subscribe(handler);
}
public static void Unsubscribe<T>(Action<T> handler) where T : GameEvent
{
EventManagerInternal<T>.Unsubscribe(handler);
}
public static void Raise<T>(T e) where T : GameEvent
{
EventManagerInternal<T>.Raise(e);
}
private static class EventManagerInternal<T> where T : GameEvent
{
private static Dictionary<Type, Action<T>> dic = new Dictionary<Type, Action<T>>();
public static void Subscribe(Action<T> handler)
{
// sub code...
}
public static void Unsubscribe(Action<T> handler)
{
// unsub code...
}
public static void Raise(T e)
{
// raise code...
}
}
}
Example usage: 用法示例:
public class OnRename : GameEvent { }
public void OnRenameHandler(OnRename e) { }
EventManager.Subscribe<OnRename>(OnRenameHandler); // <-- the statement that I wanna generate via reflection
My question is: I want to generate the same thing (same usage example like ^) via reflection but I'm unable to. 我的问题是:我想通过反射生成相同的东西(例如^的相同用法示例),但我无法这样做。 How to do it?
怎么做?
I managed to get the subscribe method right: 我设法使订阅方法正确:
MethodInfo subscribe = typeof(EventManager).GetMethod("Subscribe").MakeGeneric(typeof(GameEvent)); // right?
But then how to invoke it passing in OnRenameHandler
? 但那么如何调用它传入
OnRenameHandler
呢?
Knowing that I have a MethodInfo
to OnRenameHandler
知道我对
OnRenameHandler
有一个MethodInfo
MethodInfo handler = typeof(SomeType).GetMethod("OnRenameHandler");
subscribe.Invoke(null, WHAT_TO_PASS_HERE?);
I tried Delegate.CreateDelegate
but didn't get anywhere, I'm not sure it's the solution. 我尝试了
Delegate.CreateDelegate
但没有成功,我不确定这是否是解决方案。
I've looked at several links, tried several things, but none worked. 我看了几个链接,尝试了几件事,但是都没有用。
Thanks for any help :) 谢谢你的帮助 :)
Use Delegate.CreateDelegate
static method to create delegate with a type known at runtime. 使用
Delegate.CreateDelegate
静态方法创建具有运行时已知类型的委托。 First parameter sets the delegate type. 第一个参数设置委托类型。
// get subscribe method info
var subscribe = typeof(EventManager).GetMethod("Subscribe")
.MakeGenericMethod(typeof(OnRename));
// prepare delegate instance
var delegateType = typeof(Action<>).MakeGenericType(typeof(OnRename));
var methodInfo = typeof(TypeWithOnRenameHandlerMethod).GetMethod("OnRenameHandler");
var del = Delegate.CreateDelegate(delegateType, this, methodInfo);
// invoke subscribe method
subscribe.Invoke(null, new [] { del });
Replace this
as second Delegate.CreateDelegate
parameter if you need to call the method on other then current instance. 更换
this
为第二Delegate.CreateDelegate
如果需要调用的方法对其他则当前的实例参数。
PS. PS。 I assumed that you really need to call
Subscribe<OnRename>
, not Subscribe<GameEvent>
. 我假设您确实需要调用
Subscribe<OnRename>
,而不是Subscribe<GameEvent>
。 Feel free to change it back if I'm wrong. 如果我错了,请随时将其改回。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.