我有此接口及其实现:
public interface IInterface<TParam>
{
void Execute(TParam param);
}
public class Impl : IInterface<int>
{
public void Execute(int param)
{
...
}
}
如何使用typeof(Impl)的反射获取TParam(int here)类型?
您可以使用一些反射:
// your type
var type = typeof(Impl);
// find specific interface on your type
var interfaceType = type.GetInterfaces()
.Where(x=>x.GetGenericTypeDefinition() == typeof(IInterface<>))
.First();
// get generic arguments of your interface
var genericArguments = interfaceType.GetGenericArguments();
// take the first argument
var firstGenericArgument = genericArguments.First();
// print the result (System.Int32) in your case
Console.WriteLine(firstGenericArgument);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句