检查类是否来自泛型类我的项目中有一个带有派生类的泛型类。public class GenericClass<T> : GenericInterface<T>{}public class Test : GenericClass<SomeType>{}有没有办法找出Type对象派生自GenericClass?t.IsSubclassOf(typeof(GenericClass<>))不起作用。
3 回答
慕的地10843
TA贡献1785条经验 获得超8个赞
static bool IsSubclassOfRawGeneric(Type generic, Type toCheck) {
while (toCheck != null && toCheck != typeof(object)) {
var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
if (generic == cur) {
return true;
}
toCheck = toCheck.BaseType;
}
return false;}
郎朗坤
TA贡献1921条经验 获得超9个赞
public static bool InheritsOrImplements(this Type child, Type parent){
parent = ResolveGenericTypeDefinition(parent);
var currentChild = child.IsGenericType
? child.GetGenericTypeDefinition()
: child;
while (currentChild != typeof (object))
{
if (parent == currentChild || HasAnyInterfaces(parent, currentChild))
return true;
currentChild = currentChild.BaseType != null
&& currentChild.BaseType.IsGenericType
? currentChild.BaseType.GetGenericTypeDefinition()
: currentChild.BaseType;
if (currentChild == null)
return false;
}
return false;}private static bool HasAnyInterfaces(Type parent, Type child){
return child.GetInterfaces()
.Any(childInterface =>
{
var currentInterface = childInterface.IsGenericType
? childInterface.GetGenericTypeDefinition()
: childInterface;
return currentInterface == parent;
});}private static Type ResolveGenericTypeDefinition(Type parent){
var shouldUseGenericType = true;
if (parent.IsGenericType && parent.GetGenericTypeDefinition() != parent)
shouldUseGenericType = false;
if (parent.IsGenericType && shouldUseGenericType)
parent = parent.GetGenericTypeDefinition();
return parent;}protected interface IFooInterface{}protected interface IGenericFooInterface<T>{}protected class FooBase{}protected class FooImplementor
: FooBase, IFooInterface{}protected class GenericFooBase
: FooImplementor, IGenericFooInterface<object>{}protected class GenericFooImplementor<T>
: FooImplementor, IGenericFooInterface<T>{}[Test]public void Should_inherit_or_implement_non_generic_interface(){
Assert.That(typeof(FooImplementor)
.InheritsOrImplements(typeof(IFooInterface)), Is.True);}[Test]public void Should_inherit_or_implement_generic_interface(){
Assert.That(typeof(GenericFooBase)
.InheritsOrImplements(typeof(IGenericFooInterface<>)), Is.True);
}[Test]public void Should_inherit_or_implement_generic_interface_by_generic_subclass(){
Assert.That(typeof(GenericFooImplementor<>)
.InheritsOrImplements(typeof(IGenericFooInterface<>)), Is.True);
}[Test]public void Should_inherit_or_implement_generic_interface_by_generic_subclass_not_caring_about_generic_type_parameter(){
Assert.That(new GenericFooImplementor<string>().GetType()
.InheritsOrImplements(typeof(IGenericFooInterface<>)), Is.True);
}[Test]public void Should_not_inherit_or_implement_generic_interface_by_generic_subclass_not_caring_about_generic_type_parameter(){
Assert.That(new GenericFooImplementor<string>().GetType()
.InheritsOrImplements(typeof(IGenericFooInterface<int>)), Is.False);
}[Test]public void Should_inherit_or_implement_non_generic_class(){
Assert.That(typeof(FooImplementor)
.InheritsOrImplements(typeof(FooBase)), Is.True);}[Test]public void Should_inherit_or_implement_any_base_type(){
Assert.That(typeof(GenericFooImplementor<>)
.InheritsOrImplements(typeof(FooBase)), Is.True);}- 3 回答
- 0 关注
- 464 浏览
添加回答
举报
0/150
提交
取消
