【发布时间】:2015-10-01 15:29:07
【问题描述】:
具体来说,我正在研究一堆像这样重载的扩展方法(如果你猜不到的话,它们是 SignalR 的一部分):
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);
现在我可以通过这样做来获取第一个(非通用)On 方法的 methodInfo:
var methodInfo = typeof(HubProxyExtensions).GetMethod("On", new[] {typeof(IHubProxy), typeof(string), typeof(Action)});
但是,我希望能够获得“On”方法的第二个或第三个定义。但是,我发现这样的事情不工作:
var methodInfo = typeof(HubProxyExtensions).GetMethod("On", new[] {typeof(IHubProxy), typeof(string), typeof(Action<>)});
在上述情况下,methodInfo 最终为空。有什么想法吗?
【问题讨论】:
-
它不起作用,因为你在寻找
Action<>,而参数的类型是Action<T1, T2>。你需要一个自定义的Binder。
标签: c# reflection