views:

1236

answers:

3

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!

+7  A: 

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

typeof(B).IsAssignableFrom(c.GetType())
280Z28
Perfect!
Giffyguy
The answer from cdm9002 is better, since it doesn't use reflection.
pb
@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. :)
280Z28
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.
+15  A: 

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
cdm9002
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.
280Z28
Ah, true enough. You are correct, my question is a bit fuddled there. My bad.
Giffyguy
+1 This is the right solution to this problem.
Steven Sudit
+3  A: 

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

Steven Sudit
Indeed. Put a virtual method on class A, override as appropriate in subclasses, then call this virtual method.
Jeremy McGee
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
Ok, then using "is" should suffice.
Steven Sudit