jeudi 1 octobre 2015

Use Reflection to Get a Method which Takes Action

Specifically, I am looking at a bunch of extension methods which are all overloaded like this (they're part of SignalR, if you couldn't guess):

public static IDisposable On(this IHubProxy proxy, string eventName, Action onData);
public static IDisposable On<T>(this IHubProxy proxy, string eventName, Action<T> onData);
public static IDisposable On<T1, T2>(this IHubProxy proxy, string eventName, Action<T1, T2> onData);

Now I have no problem getting the methodInfo for the first (non-generic) On method by doing this:

var methodInfo = typeof(HubProxyExtensions).GetMethod("On", new[] {typeof(IHubProxy), typeof(string), typeof(Action)});

However, I want to be able to get the second or third definition of the "On" method. However, I've found that something like this does not work:

var methodInfo = typeof(HubProxyExtensions).GetMethod("On", new[] {typeof(IHubProxy), typeof(string), typeof(Action<>)});

In the above case, methodInfo ends up being null. Any ideas?





Aucun commentaire:

Enregistrer un commentaire