简体   繁体   English

通过行动 <T> 通过反射的通用方法?

[英]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.

相关问题 如何创建动作 <T> 形成通过反射给出的对象方法? - How to create an Action<T> form an object method given via Reflection? 将从Reflection获得的类传递给通用方法 - Pass a class gotten from Reflection to a generic method 通过采取Action的反射调用方法 <T> 。 所有方法,包含类和T都标记为内部 - calling method via reflection that takes an Action<T>. All of the method, the containing class and T are marked internal 如何使用 Action 调用泛型方法<t>在 C# 中使用反射的参数</t> - How to invoke generic method with Action<T> parameter using reflection in C# 如何使用反射将“ MyTypes”的每个列表传递给约束为T:MyType的通用方法? - How can I use reflection to pass each list of “MyTypes” to a generic method with a constraint of T:MyType? C#反射:为通用方法提供T. - C# reflection: providing T for a generic method 使用动作执行方法 <T> 使用反射的论点 - Execute method with Action<T> argument using Reflection Action的通用方法 <T> 参数 - Generic method with Action<T> parameter 如何创建动作 <T> 与反射,其中T是发现的泛型类型 - How to create an Action<T> with Reflection where T is a discovered generic Type 通过派生类中的.NET反射调用受保护的泛型方法 - Call protected generic method via .NET reflection within derived class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM