C
C#2y ago
Hugh

✅ Getting generic type parameters using reflection

Using the reflection system, how can I get at the generic type parameters? I have the following:
public class MyClass<T1, T2> {}

Type myClassType = typeof(MyClass<string, int>);
Type myClassGenericType = myClassType.GetGenericTypeDefinition();
public class MyClass<T1, T2> {}

Type myClassType = typeof(MyClass<string, int>);
Type myClassGenericType = myClassType.GetGenericTypeDefinition();
And from myClassGenericType, I want to get details of what the generic type parameters are (T1 and T2). In the debugger, this information is in GenericTypeParameters on RuntimeType, but this isn't part of Type, and RuntimeType is private, so I can't get at its internals.
2 Replies
Aaron
Aaron2y ago
GetGenericArguments()?
Hugh
HughOP2y ago
Oh god, of course... I've used that when working with concrete types, but didn't realise the same thing would work with the generic types Thanks!
Want results from more Discord servers?
Add your server