I want to elaborate on Jb Evain's answer, about how to pass parameters to the attribute. For the sample, I used System.ComponentModel.BrowsableAttribute
and passed the vlaue of browsable
parameter to its constructor:
void AddBrowsableAttribute(
ModuleDefinition module,
Mono.Cecil.ICustomAttributeProvider targetMember, // Can be a PropertyDefinition, MethodDefinition or other member definitions
bool browsable)
{
// Your attribute type
var attribType = typeof(System.ComponentModel.BrowsableAttribute);
// Find your required constructor. This one has one boolean parameter.
var constructor = attribType.GetConstructors()[0];
var constructorRef = module.ImportReference(constructor);
var attrib = new CustomAttribute(constructorRef);
// The argument
var browsableArg =
new CustomAttributeArgument(
module.ImportReference(typeof(bool)),
browsable);
attrib.ConstructorArguments.Add(browsableArg);
targetMember.CustomAttributes.Add(attrib);
}
Also, named arguments can be added to Properties
collection of the created CustomAttribute
.