Tom Deloford
Tom Deloford

Reputation: 2165

How to determine using reflection the generic parameter of the base class

I have the following structure

public class MyClass : MyBaseClass<System.Int32>
{
}

In a static method and without instantiating a new MyClass instance how do I get the type of the generic parameter used to build the concrete base class? e.g in the above example System.Int32

Upvotes: 1

Views: 747

Answers (2)

JaredPar
JaredPar

Reputation: 754515

Try this

public static Type GetBaseTypeGenericArgument(Type type) {
  return type.BaseType.GetGenericArguments()[0];
}

...
GetBaseTypeGenericArgument(typeof(MyClass));

Upvotes: 5

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

Type arg = typeof(MyClass).BaseType.GetGenericArguments()[0];

Upvotes: 0

Related Questions