Skip to content

Instantly share code, notes, and snippets.

@kazyx
kazyx / GetTypeOfGenericArg.cs
Last active January 28, 2017 04:00
How to detect Type of generic property, in other words generic argument, from derived class
public class Main {
public Type GetTypeOfGenericArg(Type derivedClassType) {
var typedMyInterfaceType = derivedClassType.GetInterface(typeof(MyInterface<>).FullName);
// If derivedClassType is 'MyFoo', typedMyInterfaceType is typeof(MyInterface<Foo>)
return typedMyInterfaceType.GetGenericArguments()[0];
}
public Type GetTypeOfGenericArgByPropertyName(Type derivedClassType) {
var myParamPropertyInfo = derivedClassType.GetProperty(nameof(MyInterface.MyParameter));