1
var type1 = typeof (ClassA);
var type2 = typeof (ClassB);

is type2 derived from type1 ?

bool isDerived = // code....
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
apocalypse
  • 5,764
  • 9
  • 47
  • 95
  • 2
    With almost a hundred questions from you, we expect you to be able to write an actual question and format code properly by now. – BoltClock Oct 25 '13 at 10:15

4 Answers4

9
var type1 = typeof(ClassA);
var type2 = typeof(ClassB);
bool isDerived = type2.IsSubClassOf(type1);

Reference: Type.IsSubclassOf Method

Son Huy TRAN
  • 486
  • 2
  • 5
  • 16
  • 3
    Just a quick note, the context is not clear from the question, but this will return `false` if `type2 == type1`. And while, strictly speaking, the question is for checking _derived_ types, depending on how zgnilec is planning to use this, it may be an issue. – Chris Sinclair Oct 25 '13 at 10:25
1
void Main()
{
    var type1 = typeof (ClassA);
    var type2 = typeof (ClassB);
    bool b = type1.IsAssignableFrom(type2);
    Console.WriteLine(b);
}

    class ClassA {}
    class ClassB : ClassA {}

Behaviour of IsAssignableFrom

true if c and the current Type represent the same type, or if the current Type is in the inheritance hierarchy of c, or if the current Type is an interface that c implements, or if c is a generic type parameter and the current Type represents one of the constraints of c, or if c represents a value type and the current Type represents Nullable (Nullable(Of c) in Visual Basic). false if none of these conditions are true, or if c is null.

Jaycee
  • 3,098
  • 22
  • 31
  • Note that `IsAssignableFrom` will also check for interfaces, not strictly just _derived_ types. That is, if `type1` is an interface that `type2` implements, it will return `true`. However, for zgnilec's actual usage, this may be desired (or inconsequential) behaviour. – Chris Sinclair Oct 25 '13 at 10:28
  • @ChrisSinclair True, I've added that information – Jaycee Oct 25 '13 at 10:31
1

You can check the Type.Basetype (see here) to see, which types you inherit from.

So you could write something like:

bool isDerived = type2.BaseType == type1;

Thanks to Daniel for pointing out my error with typeof!

germi
  • 4,628
  • 1
  • 21
  • 38
  • -1. That is wrong on quite a lot of levels: 1. `typeof` only works on class names and not on variables, so this code won't compile. 2. The type of a variable of `System.Type` is - guess what - `System.Type`, so this kind of comparison would always yield true. 3. Using `BaseType` you can only check for direct parent-child relationship. – Daniel Hilgarth Oct 25 '13 at 10:19
  • Ouch. You're right with the typeof, thanks! To your third point: You can go 'higher' by checking the `BaseType` of the `BaseType` until you reach an end, so you will get to all Types in the object's line of inheritance. But you're right that I don't get e. g. interfaces. – germi Oct 25 '13 at 10:22
  • A much better approach would be to just use the methods that already exist to check this. But my third point wasn't the reason for the downvote. The two other points were. – Daniel Hilgarth Oct 25 '13 at 10:29
  • You're right. `IsSubClassOf` is the better way to do it. – germi Oct 25 '13 at 10:30
1

If your intent is to check that Type2 is a class that is derived from Type1, the Type.IsSubclassOf method may be suitable. It returns true:

if the Type represented by the c parameter and the current Type represent classes, and the class represented by the current Type derives from the class represented by c; otherwise, false. This method also returns false if c and the current Type represent the same class.

In your example, isDerived could be expressed as:

isDerived = type2.IsSubclassOf(type1)
drf
  • 8,461
  • 32
  • 50