c# - Is there a way to get value of constructor parameter at runtime -


what i'm trying copy existing attributes 1 property another. here code now:

foreach (var prop in typeof(example).getproperties()) {             fieldbuilder field = typebuilder.definefield("_" + prop.name, prop.propertytype, fieldattributes.private);              propertybuilder propertybuilder =                 typebuilder.defineproperty(prop.name,                                  propertyattributes.hasdefault,                                  prop.propertytype,                                  null);              object[] attributes = prop.getcustomattributes(true);             foreach (var attr in attributes)             {                 //here need value of constructor parameter passed in declaration of example class                 constructorinfo attributeconstructorinfo = attr.gettype().getconstructor(new type[]{});                 customattributebuilder customattributebuilder = new customattributebuilder(attributeconstructorinfo,new type[]{});                 propertybuilder.setcustomattribute(customattributebuilder);             } } 

it's working attributes have parameterless constructor. e.g. "datatypeattribute" have constructors parameter.

now wondering if there way current value of attribute constructor

let's have model:

public class example {     public virtual int id { get; set; }     [required]     [maxlength(50)]     [datatype(datatype.text)]     public virtual string name { get; set; }     [maxlength(500)]     public virtual string desc { get; set; }     public virtual string startdt { get; set; }      public example()     {      } } 

for i'm able copy requiredattribute because has parameterless constructor. i'm unable copy datatypeattribute. value datatype.text example model.

anyone have ideas how make work?

instead of getcustomattributes(), returns constructed attributes, use getcustomattributesdata(). returns collection of customattributedata, contains need: constructor used create attribute, arguments , information named arguments of attribute.


Comments