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