vendredi 31 mars 2017

How to get generic argument name via reflection

given generic class

class Foo<TData> { }

and concrete type

var type = typeof(Foo<string>) //Fpp`[System.String]
var genericType = type.GetGenericTypeDefinition(); //Foo`1[TData]

I would like to get "TData". Or the other way: Get the value of the generic argument based on generic argument name:

GetGenericArgumentType(typeof(Foo<string>), "TData") //should return System.String





Aucun commentaire:

Enregistrer un commentaire