为了账号安全,请及时绑定邮箱和手机立即绑定

检查类是否来自泛型类

检查类是否来自泛型类

C#
守着一只汪 2019-07-08 16:00:00
检查类是否来自泛型类我的项目中有一个带有派生类的泛型类。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;}


查看完整回答
反对 回复 2019-07-08
?
郎朗坤

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);}


查看完整回答
反对 回复 2019-07-08
  • 3 回答
  • 0 关注
  • 433 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信