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

19,784

Solution 1

You can just use is:

if (c is B) // Will be true

if (d is B) // Will be false

Solution 2

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())

Solution 3

This looks like a job for polymorphism, as opposed to a big switch statement with tests for specific classes.

Solution 4

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.

I used Sam's solution, and was pleasantly surprised when Resharper made this suggestion.

Share:
19,784
Giffyguy
Author by

Giffyguy

I enjoy writing hardcore OO data-structures in native C++.

Updated on June 15, 2022

Comments

  • Giffyguy
    Giffyguy about 2 years

    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. How can this be accomplished?

  • Sam Harwell
    Sam Harwell almost 15 years
    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.
  • Paul van Brenk
    Paul van Brenk almost 15 years
    The answer from cdm9002 is better, since it doesn't use reflection.
  • Giffyguy
    Giffyguy almost 15 years
    Ah, true enough. You are correct, my question is a bit fuddled there. My bad.
  • Sam Harwell
    Sam Harwell almost 15 years
    @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. :)
  • Jeremy McGee
    Jeremy McGee almost 15 years
    Indeed. Put a virtual method on class A, override as appropriate in subclasses, then call this virtual method.
  • Giffyguy
    Giffyguy almost 15 years
    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.
  • Steven Sudit
    Steven Sudit almost 15 years
    Ok, then using "is" should suffice.
  • petr k.
    petr k. almost 15 years
    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.