0

Friends,

Given these three classes:

public class MyBaseClass {
}

public class MyDerivedClass1: MyBaseClass {
}

public class MyDerivedClass2: MyBaseClass {
}

At runtime, I will have an object of type MyBaseClass (which will either be MyDerivedClass2 or MyDerivedClass1). How can I determine, at runtime if MyBaseClass is MyDerivedClass1?

Alan Ford
  • 365
  • 1
  • 4
  • 15

3 Answers3

5

Using the is operator.

is (C# Reference) - Checks if an object is compatible with a given type. For example, the following code can determine if an object is an instance of the MyObject type, or a type that derives from MyObject:

The is operator returns true if an instance is in the inheritance tree.

if (myInstance is MyDerivedClass1) 
{
    // my instance is of type MyDerivedClass1
}

This will work with your sample code. BUT if you want to check if its exactly the same type you need to use typeof() and GetType().

if (myInstance.GetType() == typeof (MyDerivedClass1))
{
    // my instance is of type MyDerivedClass1
}

More Information

Community
  • 1
  • 1
dknaack
  • 60,192
  • 27
  • 155
  • 202
1

1. If you want to be sure whether the instance is exactly of the given type you should use GetType() method and compare it with desired type:

bool IsExactlyOfMyDerivedClass2(object instance)
{
    if (instance == null)
        throw new ArgnumentNullException();

    return (instance.GetType() == typeof(MyDerivedClass2))
}

or generic version

bool IsExactlyOf<T>(object instance)
{
    if (instance == null)
        throw new ArgnumentNullException();

    return (object.GetType() == typeof(T))
}

2. If you does not care whether the instance is exactly of the given type (or the type is abstract class or interface) you, as it has been pointed by @dknaack use the IS C# operator:

bool IsOfMyDerivedClass2_OrMoreDerived(object instance)
{
    if (instance == null)
        throw new ArgnumentNullException();

    return instance is MyDerivedClass;
}

3. Also, you can also use the IsAssignable method of Type class:

bool IsOfMyDerivedClass2_OrMoreDerived(object instance)
{
    if (instance == null)
        throw new ArgnumentNullException();

    return typeof(MyDerivedClass2).IsAssignableFrom(instance.GetType());
}

or generic version:

bool IsOf_OrMoreDerived<T>(object instance)
{
    if (instance == null)
        throw new ArgnumentNullException();

    return typeof(T).IsAssignableFrom(instance.GetType());
}
Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53
  • a couple things, `object.GetType()` should be `instance.GetType()` and then you can just return the result of your final if statement – Sayse Aug 07 '14 at 13:27
0

If you don't know the types at compile time (ie you only have access to Type objects), you can use Type.IsAssignableFrom (or TypeInfo.IsAssignableFrom depending on which Platform you are).

var baseClassType = typeof(BaseClass);
var derivedClassType = typeof(DerivedClass);
var isBaseClass = baseClassType.IsAssignableFrom(derivedClassType);
Lucas Loegel
  • 101
  • 3