class Abc {
public Mycollection<Person> Persons { get;set; }
}
class MyCollection<T> : ICollect<T> { ... }
我正在使用反射,获得ABC.Persons的PropertyInfo .
我想知道如果PropertyInfo是ICollect<>类型-我怎么做?
这似乎类似于:如何检测类型是否为另一个泛型
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);
}