class Abc {
public Mycollection<Person> Persons { get;set; }
}
class MyCollection<T> : ICollect<T> { ... }
我正在使用反射,获取ABC.Persons的PropertyInfo。
我想知道PropertyInfo是否属于ICollect&lt;&gt;类型 - 我该怎么做?
答案 0 :(得分:1)
这类似于:How To Detect If Type is Another Generic Type
public static bool IsAssignableToGenericType(Type givenType, Type genericType) {
var interfaceTypes = givenType.GetInterfaces();
foreach (var it in interfaceTypes)
if (it.IsGenericType)
if (it.GetGenericTypeDefinition() == genericType) return true;
Type baseType = givenType.BaseType;
if (baseType == null) return false;
return baseType.IsGenericType &&
baseType.GetGenericTypeDefinition() == genericType ||
IsAssignableToGenericType(baseType, genericType);
}