Take the 2-minute tour ×

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. (C is a B, but B is not necessarily a 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? I don't care if it is an object DERIVED from B, so long as the base B class is there. 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. Does anyone know how this can be accomplished?

Thanks in advance for your advice!

share|improve this question
You should avoid this kind of logic where possible. When you have to determine what the derived type of an object is, you're not making good use of polymorphism. –  Tim Sylvester Aug 2 '09 at 5:39
See my comment on Steven Sudit's answer. –  Giffyguy Aug 2 '09 at 17:15

4 Answers 4

up vote 23 down vote accepted

Am I missing some subtlety in your question, can't you just use is?

if (c is B) // will be true

if (d is B) // will be false
share|improve this answer
As a response to the only actual question in the OP: "How can I tell if an object is a B or anything derived from it?" this is the correct answer. Mine is the answer to the thread title, but less appropriate for the described problem. –  Sam Harwell Aug 2 '09 at 4:59
Ah, true enough. You are correct, my question is a bit fuddled there. My bad. –  Giffyguy Aug 2 '09 at 5:03
+1 This is the right solution to this problem. –  Steven Sudit Aug 2 '09 at 19:15

Edit: this answers the question in the thread title. cdm9002 has the better answer to the problem as described in the full post.

share|improve this answer
The answer from cdm9002 is better, since it doesn't use reflection. –  pb. Aug 2 '09 at 5:00
@pb: If you want to get technical, the other answer doesn't use typeof with inheritance. As you can see from my edit, I'm not one to claim the first answer I thought of is the most appropriate. :) –  Sam Harwell Aug 2 '09 at 5:04
This is not only a perfectly correct answer, but also in addition covers the case when you do not have instances of any of the the classes at hand, but still want to determine whether there is an inheritance relationship between two types. –  petr k. Aug 5 '09 at 16:35

May I politely suggest that this looks like a job for polymorphism, as opposed to a big switch statement with tests for specific classes?

share|improve this answer
Indeed. Put a virtual method on class A, override as appropriate in subclasses, then call this virtual method. –  Jeremy McGee Aug 2 '09 at 5:49
How rude!! Haha. My class logic doesn't actually look like this mess. I just needed to figure out how to perform this condition. I'm writing a PropertyChangedCallback method IN the abstract base class, and I need a security condition that will ensure that the DependencyObject parameter is an object derived from my base class. If not, I throw an error or cancel my logic. Polymorphism isn't helpful in this instance. –  Giffyguy Aug 2 '09 at 17:14
Ok, then using "is" should suffice. –  Steven Sudit Aug 2 '09 at 19:14

Sorry for reviving old thread, but think this might help someone ...

For Vb.Net with VS2008, you can check like

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

End If
share|improve this answer

Your Answer


