我怎么知道PropertyInfo是否为icolllect <>type -反射和泛型

本文关键字:type 反射 泛型 PropertyInfo 是否 icolllect 我怎么知道 | 更新日期: 2023-09-27 18:11:48

class Abc {
    public Mycollection<Person> Persons { get;set; }
}
class MyCollection<T> : ICollect<T> { ... }

我正在使用反射,获得ABC.Persons的PropertyInfo .

我想知道如果PropertyInfo是ICollect<>类型-我怎么做?

我怎么知道PropertyInfo是否为icolllect <>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);

}