How to Test If a Instance of a Class Is a Specific Generic Type

Testing if object is of generic type in C#

If you want to check if it's an instance of a generic type:

return list.GetType().IsGenericType;

If you want to check if it's a generic List<T>:

return list.GetType().GetGenericTypeDefinition() == typeof(List<>);

As Jon points out, this checks the exact type equivalence. Returning false doesn't necessarily mean list is List<T> returns false (i.e. the object cannot be assigned to a List<T> variable).

Determine if object is an instance of a generic base class, any generic type

The problem is that DrevidedC1 is not a sublcass of Class1<T>, it's a subclass of Class1<int>. Make sure you understand this subtle diference; Class1<T> is a open type (T can be anything, it hasn't been set) while DerivedC1 extends a closed type Class1<int> (it's not open in T anymore, T is set to int and only int). So when you do the following:


The answer is evidently false.

What you need to do is check if the generic type definition of DerivedC1's base type (think of it as the corresponding open generic type of Class1<int>) equals Class1<T> which it clearly does.

The correct code is therefore:

typeof(DerivedC1).BaseType.GetGenericTypeDefinition() == typeof(Class1<>));

Or better yet, as Matías Fidemraizer states in his answer:


Check if a class is generic type

From the Class<?> object you can check cls.getTypeParameters().

From a generic class you get a nonempty array of TypeVariable.

From a non-generic class you get an empty array.


TypeVariable[1] { E }
TypeVariable[0] {}

Check if a class is derived from a generic class

Try this code

static bool IsSubclassOfRawGeneric(Type generic, Type toCheck) {
while (toCheck != null && toCheck != typeof(object)) {
var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
if (generic == cur) {
return true;
toCheck = toCheck.BaseType;
return false;

how to compare type of an object's instance to a generic type?

Basically you can't do that due to type erasure. The normal workaround is to pass a Class object as a parameter; e.g.

    public <U extends IComponent> U GetComponent(Class<U> clazz) {
for (IComponent component : list) {
if (clazz.isInstance(component)) {
return clazz.cast(component);

You could also use if (clazz.equals(component.getClass())) { ... but that does an exact type match ... which is not what the instanceof operator does. The instanceof operator and the Class.instanceOf method both test to see if the value's type is assignment compatible.

Test if Object is a generic list of specific type

If you can't influence the signature of your method or the Iterable instances you get, then there is no way to distinguish an instance of e.g. Iterable<Object> containing only integers from an instance of Iterable<Integer>. They are identical in every respect; in fact, the very concept "instance of Iterable<Integer>" is nebulous for this reason. Instances are runtime artifacts and constructor type parameters are a compile-time artifact.

How to check if a generic type implements a specific type of generic interface in java?

Java implements erasure, so there's no way to tell on runtime if genericObject is an instance of Set<String> or not. The only way to guarantee this is to use bounds on your generics, or check all elements in the set.

Compile-time Generic Bounds

Using bounds checking, which will be checked at compile-time:

public <T extends SomeInterface> void genericMethod(Set<? extends T> tSet) {
// Do something with tSet here
Java 8

We can use streams in Java 8 to do this natively in a single line:

public <T> void genericMethod(T t) {
if (t instanceof Set<?>) {
Set<?> set = (Set<?>) t;
if ( {
Set<String> strs = (Set<String>) set;
// Do something with strs here
Java 7 and older

With Java 7 and older, we need to use iteration and type checking:

public <T> void genericMethod(T t) {
Set<String> strs = new HashSet<String>();
Set<?> tAsSet;
if (t instanceof Set<?>) {
tAsSet = (Set<?>) t;
for (Object obj : tAsSet) {
if (obj instanceof String) {
strs.add((String) obj);
// Do something with strs here
} else {
// Throw an exception or log a warning or something.

As per Mark Peters' comment below, Guava also has methods that do this for you if you can add it to your project:

public <T> void genericMethod(T t) {
if (t instanceof Set<?>) {
Set<?> set = (Set<?>) t;
if (Iterables.all(set, Predicates.instanceOf(String.class))) {
Set<String> strs = (Set<String>) set;
// Do something with strs here

The statement, Iterables.all(set, Predicates.instanceOf(String.class)) is essentially the same thing as set instanceof Set<String>.

Unit testing when generic type is needed

Currently AutoFixture doesn't support non-closed generic types activation and it's unlikely it will change one day.

Firstly it's caused by the fact that the C# language itself supports non-closed generic types for the reflection purpose only. You cannot declare variable of MyClass<> type without closing the type over particular T. Hence, even if AutoFixture substitute type and activate an instance for you, it will be hard to work with the result - you'd either need to use a variable of object type or rely on covariance if your type supports that. None of those options looks usable.

Secondly, the task itself is very hard as generic type could have constrains. Sometimes constrains could get crazy and e.g. have cyclic dependencies: class MyClass<T1, T2> where T1 : T2 where T2 : IEnumerable<T1>. The AutoFixture.Idioms library already tries to solve that problem for the GuardClauseAssertion, but the success is average. This feature requires dynamic type generation (which BTW is not supported on all the platforms) and very advanced usage of the Reflection.Emit API, which makes it very hard to implement correctly.

Related Topics

Leave a reply