Check if a class is derived from a generic class

前端 未结 16 1569
太阳男子
太阳男子 2020-11-22 09:57

I have a generic class in my project with derived classes.

public class GenericClass : GenericInterface
{
}

public class Test : GenericCla         


        
16条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 10:22

    You can try this extension

        public static bool IsSubClassOfGenericClass(this Type type, Type genericClass,Type t)
        {
            return type.IsSubclassOf(genericClass.MakeGenericType(new[] { t }));
        }
    

提交回复
热议问题