猿问

检查类是否来自泛型类

检查类是否来自泛型类

我的项目中有一个带有派生类的泛型类。

public class GenericClass<T> : GenericInterface<T>{}public class Test : GenericClass<SomeType>{}

有没有办法找出Type对象派生自GenericClass?

t.IsSubclassOf(typeof(GenericClass<>))

不起作用。


守着一只汪
浏览 434回答 3
3回答

慕的地10843

试试这段代码static&nbsp;bool&nbsp;IsSubclassOfRawGeneric(Type&nbsp;generic,&nbsp;Type&nbsp;toCheck)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(toCheck&nbsp;!=&nbsp;null&nbsp;&&&nbsp;toCheck&nbsp;!=&nbsp;typeof(object))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;cur&nbsp;=&nbsp;toCheck.IsGenericType&nbsp;?&nbsp;toCheck.GetGenericTypeDefinition()&nbsp;:&nbsp;toCheck; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(generic&nbsp;==&nbsp;cur)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;true; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;toCheck&nbsp;=&nbsp;toCheck.BaseType; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;false;}

郎朗坤

我仔细研究了其中的一些样本,发现它们在某些情况下是缺乏的。此版本适用于各种泛型:类型、接口及其类型定义。public&nbsp;static&nbsp;bool&nbsp;InheritsOrImplements(this&nbsp;Type&nbsp;child,&nbsp;Type&nbsp;parent){ &nbsp;&nbsp;&nbsp;&nbsp;parent&nbsp;=&nbsp;ResolveGenericTypeDefinition(parent); &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;currentChild&nbsp;=&nbsp;child.IsGenericType &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;?&nbsp;child.GetGenericTypeDefinition() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;child; &nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(currentChild&nbsp;!=&nbsp;typeof&nbsp;(object)) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(parent&nbsp;==&nbsp;currentChild&nbsp;||&nbsp;HasAnyInterfaces(parent,&nbsp;currentChild)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;true; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;currentChild&nbsp;=&nbsp;currentChild.BaseType&nbsp;!=&nbsp;null &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&&&nbsp;currentChild.BaseType.IsGenericType &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;?&nbsp;currentChild.BaseType.GetGenericTypeDefinition() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;currentChild.BaseType; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(currentChild&nbsp;==&nbsp;null) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;false; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;false;}private&nbsp;static&nbsp;bool&nbsp;HasAnyInterfaces(Type&nbsp;parent,&nbsp;Type&nbsp;child){ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;child.GetInterfaces() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Any(childInterface&nbsp;=> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;currentInterface&nbsp;=&nbsp;childInterface.IsGenericType &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;?&nbsp;childInterface.GetGenericTypeDefinition() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;childInterface; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;currentInterface&nbsp;==&nbsp;parent; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});}private&nbsp;static&nbsp;Type&nbsp;ResolveGenericTypeDefinition(Type&nbsp;parent){ &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;shouldUseGenericType&nbsp;=&nbsp;true; &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(parent.IsGenericType&nbsp;&&&nbsp;parent.GetGenericTypeDefinition()&nbsp;!=&nbsp;parent) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shouldUseGenericType&nbsp;=&nbsp;false; &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(parent.IsGenericType&nbsp;&&&nbsp;shouldUseGenericType) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent&nbsp;=&nbsp;parent.GetGenericTypeDefinition(); &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;parent;}以下是单元测试:protected&nbsp;interface&nbsp;IFooInterface{}protected&nbsp;interface&nbsp;IGenericFooInterface<T>{}protected&nbsp;class&nbsp;FooBase{}protected&nbsp;class&nbsp;FooImplementor &nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;FooBase,&nbsp;IFooInterface{}protected&nbsp;class&nbsp;GenericFooBase &nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;FooImplementor,&nbsp;IGenericFooInterface<object>{}protected&nbsp;class&nbsp;GenericFooImplementor<T> &nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;FooImplementor,&nbsp;IGenericFooInterface<T>{}[Test]public&nbsp;void&nbsp;Should_inherit_or_implement_non_generic_interface(){ &nbsp;&nbsp;&nbsp;&nbsp;Assert.That(typeof(FooImplementor) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.InheritsOrImplements(typeof(IFooInterface)),&nbsp;Is.True);}[Test]public&nbsp;void&nbsp;Should_inherit_or_implement_generic_interface(){ &nbsp;&nbsp;&nbsp;&nbsp;Assert.That(typeof(GenericFooBase) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.InheritsOrImplements(typeof(IGenericFooInterface<>)),&nbsp;Is.True); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}[Test]public&nbsp;void&nbsp;Should_inherit_or_implement_generic_interface_by_generic_subclass(){ &nbsp;&nbsp;&nbsp;&nbsp;Assert.That(typeof(GenericFooImplementor<>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.InheritsOrImplements(typeof(IGenericFooInterface<>)),&nbsp;Is.True); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}[Test]public&nbsp;void&nbsp;Should_inherit_or_implement_generic_interface_by_generic_subclass_not_caring_about_generic_type_parameter(){ &nbsp;&nbsp;&nbsp;&nbsp;Assert.That(new&nbsp;GenericFooImplementor<string>().GetType() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.InheritsOrImplements(typeof(IGenericFooInterface<>)),&nbsp;Is.True); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}[Test]public&nbsp;void&nbsp;Should_not_inherit_or_implement_generic_interface_by_generic_subclass_not_caring_about_generic_type_parameter(){ &nbsp;&nbsp;&nbsp;&nbsp;Assert.That(new&nbsp;GenericFooImplementor<string>().GetType() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.InheritsOrImplements(typeof(IGenericFooInterface<int>)),&nbsp;Is.False); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}[Test]public&nbsp;void&nbsp;Should_inherit_or_implement_non_generic_class(){ &nbsp;&nbsp;&nbsp;&nbsp;Assert.That(typeof(FooImplementor) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.InheritsOrImplements(typeof(FooBase)),&nbsp;Is.True);}[Test]public&nbsp;void&nbsp;Should_inherit_or_implement_any_base_type(){ &nbsp;&nbsp;&nbsp;&nbsp;Assert.That(typeof(GenericFooImplementor<>) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.InheritsOrImplements(typeof(FooBase)),&nbsp;Is.True);}
随时随地看视频慕课网APP
我要回答