![](/img/trans.png)
[英]How to check if method parameter type/return type is generic, in Roslyn?
[英]Check if method return type is based on generic parameter
當我有MethodInfo時,如何檢查返回類型是否基於通用參數?
public class GenericClass<T>
{
public class InnerClass
{
public static T A()
{
return default(T);
}
}
}
當我檢查
typeof(GenericClass<>.InnerClass).GetMethod("A").ReturnType.IsGenericParameter
我是true
但是如果我只有一個GenericClass<int>
的(封閉)MethodInfo,該怎么辦? 我是否必須走上嵌套類型的路徑並檢查是否具有IsGeneric == true
然后獲取此類型的GenericTypeDefinition,然后通過實例化打開的類型來向下移動路徑,直到可以創建開放版本的MethodInfo或是否存在更簡單(更快)的方式?
我站得住了...
鑒於:
public class GenericClass<T1>
{
public class InnerClass<T2>
{
public static Tuple<T1, T2, T3> A<T3>()
{
return null;
}
}
}
Type type = typeof(GenericClass<int>.InnerClass<long>);
var methodInfo = type.GetMethod("A");
MethodInfo method = (MethodInfo)type.Module.ResolveMethod(methodInfo.MetadataToken);
在這里被認為是一個問題...我將其用作功能:-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.