简体   繁体   English

C#.NET - 如何使用typeof()来继承?

[英]C#.NET - How can I get typeof() to work with inheritance?

I will start by explaining my scenario in code: 我将从代码中解释我的场景开始:

public class A { }

public class B : A { }

public class C : B { }

public class D { }

public class Test
{
    private A a = new A ( ) ;
    private B b = new B ( ) ;
    private C c = new C ( ) ;
    private D d = new D ( ) ;

    public Test ( )
    {
        // Evaluates to "false"
        if ( a.GetType == typeof(B) ) { } //TODO: Add Logic

        // Evaluates to "true"
        if ( b.GetType == typeof(B) ) { } //TODO: Add Logic

        // I WANT this to evaluate to "true"
        if ( c.GetType == typeof(B) ) { } //TODO: Add Logic

        // Evaluates to "false"
        if ( d.GetType == typeof(B) ) { } //TODO: Add Logic
    }
}

The important line to take notice of here is: 这里要注意的重要路线是:

if ( c.GetType == typeof(B) ) { }

I believe that this will in fact evaluate to "false", since typeof(B) and typeof(C) are not equal to each other in both directions. 我认为这实际上会评估为“假”,因为(B)类型和(C)类型在两个方向上彼此不相等。 (C is a B, but B is not necessarily a C.) (C是B,但B不一定是C.)

But what I need is some kind of condition that will take this into account. 但我需要的是某种将这一点考虑在内的条件。 How can I tell if an object is a B or anything derived from it? 如何判断对象是B还是从中派生的任何东西?

I don't care if it is an object DERIVED from B, so long as the base B class is there. 我不在乎它是否是来自B的DERIVED对象,只要基类B类存在。 And I can't anticipate what derived class might show up in my application. 而且我无法预测派生类可能会在我的应用程序中显示出来。 I just have to assume that unkown derived classes may exist in the future - and therefore I can only focus on making sure that the base class is what I am expecting. 我只需要假设将来可能存在未知的派生类 - 因此我只能专注于确保基类是我所期望的。

I need a condition that will perform this check for me. 我需要一个能够为我执行此检查的条件。 How can this be accomplished? 如何实现这一目标?

You can just use is : 你可以使用is

if (c is B) // Will be true

if (d is B) // Will be false

Edit: this answers the question in the thread title. 编辑:这回答了主题标题中的问题。 cdm9002 has the better answer to the problem as described in the full post. cdm9002对问题有更好的答案,如完整帖子中所述。

typeof(B).IsAssignableFrom(c.GetType())

这看起来像是一个多态性的工作,而不是一个带有特定类测试的大型switch语句。

As an alternative to the (c is B) check, you can also do the following: 作为(c is B)检查的替代方法,您还可以执行以下操作:

var maybeB = c as B;
if (maybeB != null) {
   // make use of maybeB
}

This is preferred in some cases since in order to make use of c as a B when using is , you would have to cast anyway. 因为为了利用这种优先在某些情况下c作为B使用的时候is ,你无论如何都会投。

For VB.NET with Visual Studio 2008, you can check it like: 对于使用Visual Studio 2008的VB.NET,您可以检查它:

'MyTextBox control is inherited by Textbox
If Ctl.GetType.Name = "MyTextBox" then    

End If
typeof(B).IsInstanceOfType(c)

Similar to the answer above from sam-harwell, sometimes you may have the type "B" in a variable, so you need to use reflection rather than the "is" operator. 与sam-harwell的上述答案类似,有时您可能在变量中使用“B”类型,因此您需要使用反射而不是“is”运算符。

I used Sam's solution, and was pleasantly surprised when Resharper made this suggestion. 我使用Sam的解决方案,当Resharper提出这个建议时,我感到惊喜。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM