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

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

问题描述

要检查,如果一个类型是在C#中另一种类型的子类,很容易:

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

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

不过,这将失败:

However, this will fail:

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

有没有什么办法来检查类型是否可以是一个基类本身的子类,或者,不使用经营者或使用扩展方法?

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?

推荐答案

显然,没有。

下面的选项:


  • 使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.type.issubclassof%28v=vs.110%29.aspx\">Type.IsSubclassOf

  • 使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.type.isassignablefrom%28v=vs.110%29.aspx\">Type.IsAssignableFrom


  • Use Type.IsSubclassOf
  • Use Type.IsAssignableFrom
  • is and as

正如你已经发现了,这是不行的,如果这两种类型是相同的,这里有一个样本 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

这表明派生基本的子类,但基础是(显然)不是本身子类。

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

现在,这将回答您的具体问题,但它也会给你误报。由于埃里克利珀已经指出了意见,而该方法确实会返回对于上述两个问题,这也将返回对于这些,你可能不希望:

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 { }

在这里,您将获得以下输出:

Here you get the following output:

True
True
True

最后一个将有指示,如果该方法的只有的回答提出的问题,即 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.

问题与在你的问题的背景是,他们会要求你操作上的对象和写入直接在code中的类型中的一种,而不是与键入的对象。

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天全站免登陆