C# 泛型判断是否为继承关系(IsSubclassof)

public static bool IsGenericSubclassOf(this Type type, Type superType)
{
    if (type.BaseType != null 
        && !type.BaseType.Equals(typeof(object))
        && type.BaseType.IsGenericType)
    {
        if (type.BaseType.GetGenericTypeDefinition().Equals(superType))
        {
            return true;
        }
        return type.BaseType.IsGenericSubclassOf(superType);
    }

    return false;
} 

 使用

typeof(AAA).IsGenericSubclassOf(typeof(A<>))

 

posted on 2019-09-19 22:18  BadTree  阅读(1845)  评论(0编辑  收藏  举报