c# - Determine if a property type is a generic type argument -



c# - Determine if a property type is a generic type argument -

say have class:

public class genericmodel<t1, t2> { public t1 model1 { get; set; } public t2 model2 { get; set; } }

how can tell using reflection type of model1 generic argument t1 , type of model2 generic argument t2?

i'm looking property or tell me model1's type maps typeof(genericmodel<,>).getgenericarguments()[0]

simple:

var model1type = typeof(genericmodel<,>).getproperty("model1").propertytype; var model2type = typeof(genericmodel<,>).getproperty("model2").propertytype;

then values of model1type.isgenericparameter & model2type.isgenericparameter both true indicating you've got generic parameter type properties.

also, model1type.name == "t1" & model2type.name == "t2".

if have specific instance, such var instance = new genericmodel<int, int>();, can generic name of property out:

var instancemodel1typename = instance .gettype() .getgenerictypedefinition() .getproperty("model1") .propertytype .name; // instancemodel1typename == "t1"

c# generics reflection

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -