如何检查类型是子类型还是对象类型? [英] How do I check if a type is a subtype OR the type of an object?

查看:17
本文介绍了如何检查类型是子类型还是对象类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要检查一个类型是否是 C# 中另一个类型的子类,很简单:

To check if a type is a subclass of another type in C#, it's easy:

typeof (SubClass).IsSubclassOf(typeof (BaseClass)); // returns true

但是,这将失败:

typeof (BaseClass).IsSubclassOf(typeof (BaseClass)); // returns false

有没有什么办法可以不使用OR运算符或使用扩展方法来检查一个类型是否是基类本身的子类OR?

Is there any way to check whether a type is either a subclass OR of the base class itself, without using an OR operator or using an extension method?

推荐答案

显然没有.

以下是选项:

正如您已经发现的那样,如果两种类型相同,这将不起作用,这是一个示例 LINQPad 程序,演示了:

As you've already found out, this will not work if the two types are the same, here's a sample LINQPad program that demonstrates:

void Main()
{
    typeof(Derived).IsSubclassOf(typeof(Base)).Dump();
    typeof(Base).IsSubclassOf(typeof(Base)).Dump();
}

public class Base { }
public class Derived : Base { }

输出:

True
False

这表明 DerivedBase 的子类,但 Base 显然不是它自己的子类.

Which indicates that Derived is a subclass of Base, but that Baseis (obviously) not a subclass of itself.

现在,这将回答您的特定问题,但它也会给您误报.正如 Eric Lippert 在评论中指出的那样,虽然该方法确实会为上述两个问题返回 True,但它也会为这些问题返回 True,而您可能不这样做'我不想:

Now, this will answer your particular question, but it will also give you false positives. As Eric Lippert has pointed out in the comments, while the method will indeed return True for the two above questions, it will also return True for these, which you probably don't want:

void Main()
{
    typeof(Base).IsAssignableFrom(typeof(Derived)).Dump();
    typeof(Base).IsAssignableFrom(typeof(Base)).Dump();
    typeof(int[]).IsAssignableFrom(typeof(uint[])).Dump();
}

public class Base { }
public class Derived : Base { }

这里你得到以下输出:

True
True
True

最后一个 True 表示,如果方法 only 回答了所提出的问题,则 uint[] 继承自 int[] 或者它们是相同的类型,这显然不是这种情况.

The last True there would indicate, if the method only answered the question asked, that uint[] inherits from int[] or that they're the same type, which clearly is not the case.

所以 IsAssignableFrom 也不完全正确.

So IsAssignableFrom is not entirely correct either.

在您的问题的上下文中,isas 的问题"是它们将要求您对对象进行操作并直接在代码,不适用于 Type 对象.

The "problem" with is and as in the context of your question is that they will require you to operate on the objects and write one of the types directly in code, and not work with Type objects.

换句话说,这不会编译:

In other words, this won't compile:

SubClass is BaseClass
^--+---^
   |
   +-- need object reference here

这也不会:

typeof(SubClass) is typeof(BaseClass)
                    ^-------+-------^
                            |
                            +-- need type name here, not Type object

这也不会:

typeof(SubClass) is BaseClass
^------+-------^
       |
       +-- this returns a Type object, And "System.Type" does not
           inherit from BaseClass

结论

虽然上述方法可能适合您的需求,但您问题的唯一正确答案(如我所见)是您需要额外检查:

Conclusion

While the above methods might fit your needs, the only correct answer to your question (as I see it) is that you will need an extra check:

typeof(Derived).IsSubclassOf(typeof(Base)) || typeof(Derived) == typeof(Base);

当然在方法中更有意义:

which of course makes more sense in a method:

public bool IsSameOrSubclass(Type potentialBase, Type potentialDescendant)
{
    return potentialDescendant.IsSubclassOf(potentialBase)
           || potentialDescendant == potentialBase;
}

这篇关于如何检查类型是子类型还是对象类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆