c# - How to add values to attribute added dynamically to property without attribute constructor(Reflection.Emit) -


i able add attribute , pass values constructor. how pass values when attribute not have constructor appropriate parameters pass. e.g. how add displayattribute using reflection.emit?

[display(order = 28, resourcetype = typeof(commonresources), name = "displaycomment")] 

hope clear enough i'm trying accomplish. if not please ask.

you use customattributebuilder. example:

var cab = new customattributebuilder(     ctor, ctorargs,     props, propvalues,     fields, fieldvalues ); prop.setcustomattribute(cab); 

(or 1 of related overloads)

in case, looks (this pure guesswork) like:

var attribtype = typeof(displayattribute); var cab = new customattributebuilder(     attribtype.getconstructor(type.emptytypes), // constructor selection     new object[0], // constructor arguments - none     new[] { // properties assign         attribtype.getproperty("order"),         attribtype.getproperty("resourcetype"),         attribtype.getproperty("name"),     },     new object[] { // values property assignment         28,         typeof(commonresources),         "displaycomment"     }); prop.setcustomattribute(cab); 

note i'm assuming order, resourcetype , name properties. if fields, there different overload.


Comments