对象入参指定泛型类型_如何得到正确的MethodInfo对象当一个类使用泛型和泛型类型参数...

I was wondering if someone might be able to demonstrate how to use Type's GetMethod() method to retrieve a MethodInfo object for the following signature:

Class.StaticMethod(T arg1, IInterface1 arg2, IEnumerable arg3)

Thanks,

Xam

解决方案MethodInfo methodInfo = typeof(Class)

.GetMethods(

BindingFlags.Public | BindingFlags.Static

)

.Where(m => m.Name == "StaticMethod")

.Where(m => m.IsGenericMethod)

.Where(m => m.GetGenericArguments().Length == 1)

.Where(m => m.GetParameters().Length == 3)

.Where(m =>

m.GetParameters()[0].ParameterType ==

m.GetGenericArguments()[0] &&

m.GetParameters()[1].ParameterType ==

typeof(IInterface1) &&

m.GetParameters()[2].ParameterType ==

typeof(IEnumerable)

)

.Single();

Note that you must then follow this with

methodInfo = methodInfo.MakeGenericMethod(new Type[] { typeof(ConcreteType) });

to close the type where ConcreteType is the type you want for the type parameter T.

I'm assuming:

class Class {

public static void StaticMethod(

T arg1,

IInterface1 arg2,

IEnumerable arg3

) { }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值