How could I know if an object is derived from a specific generic class?

Posted by Edison Chuang on Stack Overflow See other posts from Stack Overflow or by Edison Chuang
Published on 2010-12-30T09:16:45Z Indexed on 2010/12/30 9:53 UTC
Read the original article Hit count: 178

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET