How could I know if an object is derived from a specific generic class?
- by Edison Chuang
Suppose that I have an object then how could I know if the object is derived from a specific generic class. For example:
public class GenericClass<T>
{
}
public bool IsDeriveFrom(object o)
{
return o.GetType().IsSubclassOf(typeof(GenericClass)); //will throw exception here
}
please notice that the code above will throw an exception. The type of the generic class cannot be retrieved directly because there is no type for a generic class without a type parameter provided.