具体来说,我正在研究一堆像这样重载的扩展方法(它们是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)});
但是,我希望能够获得&#34; On&#34;的第二或第三个定义。方法。但是,我发现这样的事情不工作:
var methodInfo = typeof(HubProxyExtensions).GetMethod("On", new[] {typeof(IHubProxy), typeof(string), typeof(Action<>)});
在上面的例子中,methodInfo最终为null。有什么想法吗?
答案 0 :(得分:2)
在我的评论中详细说明,我已经制作了这个自定义活页夹:
using System;
using System.Linq;
using System.Reflection;
public class GenericDefinitionBinder : Binder
{
public static readonly GenericDefinitionBinder Default = new GenericDefinitionBinder();
public override PropertyInfo SelectProperty(BindingFlags bindingAttr, PropertyInfo[] match, Type returnType, Type[] indexes, ParameterModifier[] modifiers)
{
throw new NotImplementedException();
}
public override MethodBase SelectMethod(BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers)
{
return match.SingleOrDefault(m => MethodOkay(m, types));
}
private static bool MethodOkay(MethodBase method, Type[] types)
{
var pars = method.GetParameters();
if(types.Length != pars.Length) return false;
for(int i = 0; i < types.Length; i++)
{
var par = pars[i].ParameterType;
if(!(par == types[i] || (par.IsGenericType && par.GetGenericTypeDefinition() == types[i])))
{
return false;
}
}
return true;
}
public override void ReorderArgumentArray(ref object[] args, object state)
{
throw new NotImplementedException();
}
public override object ChangeType(object value, Type type, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
public override MethodBase BindToMethod(BindingFlags bindingAttr, MethodBase[] match, ref object[] args, ParameterModifier[] modifiers, System.Globalization.CultureInfo culture, string[] names, out object state)
{
throw new NotImplementedException();
}
public override FieldInfo BindToField(BindingFlags bindingAttr, FieldInfo[] match, object value, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
简单使用:
var methodInfo = typeof(HubProxyExtensions).GetMethod("On", BindingFlags.Public | BindingFlags.Static, GenericDefinitionBinder.Default, new[] {typeof(IHubProxy), typeof(string), typeof(Action<>)}, null);
答案 1 :(得分:0)
我认为没有办法使用您正在使用的当前方法执行此操作。但是,通过获取所有方法然后使用LINQ,您可以找到这样的方法:
var methodInfo = typeof(HubProxyExtensions).GetMethods().FirstOrDefault(x => x.Name == "On"
&& x.GetParameters().Count() == 3
&& x.GetParameters()[0].ParameterType == typeof(IHubProxy)
&& x.GetParameters()[1].ParameterType == typeof(string)
&& x.GetParameters()[2].ParameterType.IsGenericType
&& x.GetParameters()[2].ParameterType.GetGenericTypeDefinition() == typeof(Action<>));
(我知道有很多重复的GetParameters()
,但你明白了)
答案 2 :(得分:0)
要获得第二种方法的MethodInfo
,您需要致电MakeGenericMethod
:
MethodInfo method = typeof(HubProxyExtensions).GetMethod("On");
MethodInfo generic = method.MakeGenericMethod(typeof(string));
并为T
输入参数设置相同的Action<T>
以调用:
var onData = new Action<string>(Target);
generic.Invoke(this, new object[] { proxy, eventName, onData });
更新:抱歉,仅适用于没有第一个(On<T>
)或第三个(On
)方法的课程中的单个On<T1, T2>
方法。