Let's say we have a class:
public class X<T>{}
...and a class:
public class Y : X<int>{}
... It's easy enough to discover whether it is of a specific type
Y myY = new Y();
bool isType = myY is X<int>;
...but what if I don't want to know whether it is X<int> but rather whether it is X<T>
I see how I can look into the base types to help determine the generic type definition:
myY.GetType().BaseType.GetGenericTypeDefinition();
...but without resorting to such clumsy measures, is there a good way to identify if an object inherits from any typed implementation of a generic type definition Something like:
myY is X<T>
...

Detecting Inheritance From Generic Type Definitions
markmcgookin
What information do you want from the generic type definition You can use GetType().BaseType to get the base generic type. This is the only way to get access to the base type information for any class not just generics. However I'm not sure how much this information is going to do for you because generic classes do not mean inheritance. Given your example the type Y is derived from X<int> however if you create another type Z that derives from X<long> they are still distinct types and share no common base class (other than perhaps the base class from which X is derived).
Michael Taylor - 9/21/06