c# - Reflection get all GenericParameterAttributes -


i need genericparameterattributes generic parameter t in mclass

public class mclass<t>:ienumerable<t> t : class,icomparable, new()     { //some body here  } 

so try in way:

assembly asm = assembly.loadfrom("m.dll");             type some_type = asm.gettype("m.mclass`1");              type[] generic_args = { typeof(mytestclass) };             if (some_type.isgenerictype)             {                  console.writeline("generic type: {0}", some_type.tostring());                 console.writeline("where t : {0}", some_type.getgenericarguments()[0].genericparameterattributes);                 some_type = some_type.makegenerictype(generic_args);             } 

but in reason propherty genericparameterattributes show t must class default constructor doest't show t must have icomparable tried in way :

this worked dont forget rebuilt project after changed)

     static void main()             {                assembly asm = assembly.loadfrom("m.dll");                 type some_type = asm.gettype("m.mclass`1");                  if (some_type.isgenerictype)                 {                     var generic_argument =some_type.getgenericarguments()[0];                      var generic_parameter_constraints =generic_argument.getgenericparameterconstraints();                     console.writeline(generic_parameter_constraints.length);                     console.readline(); }      } 

you can invoke getgenericparameterconstraints on generic argument type this:

var generic_argument =     some_type     .getgenericarguments()[0]; //this give generic argument t in example.  var generic_parameter_constraints =     generic_argument     .getgenericparameterconstraints(); //this give array of types present type constraints on generic argument t 

in case generic_parameter_constraints contain array of type has single element typeof(icomparable).


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -