This question is related to this one, but is not a duplicate. Jb posted there that to add a custom attribute, the following snippet would work:
ModuleDefinition module = ...;
MethodDefinition targetMethod = ...;
MethodReference attributeConstructor = module.Import(
typeof(DebuggerHiddenAttribute).GetConstructor(Type.EmptyTypes));
targetMethod.CustomAttributes.Add(new CustomAttribute(attributeConstructor));
module.Write(...);
I would like to use something similar, but to add a custom attribute whose constructor takes two string parameters in its (only) constructor, and I'd like to specify values for those (obviously). Can anyone help?
First you have to get a reference to the proper version of the constructor:
MethodReference attributeConstructor = module.Import(
typeof(MyAttribute).GetConstructor(new [] { typeof(string), typeof(string) }));
Then you can simply populate the custom attributes with string arguments:
CustomAttribute attribute = new CustomAttribute(attributeConstructor);
attribute.ConstructorArguments.Add(
new CustomAttributeArgument(
module.TypeSystem.String, "Foo"));
attribute.ConstructorArguments.Add(
new CustomAttributeArgument(
module.TypeSystem.String, "Bar"));
Here's how to set Named Parameters of a custom attribute which totally bypasses the setting of an attribute value using it's constructors. As a note you can't set the CustomAttributeNamedArgument.Argument.Value or even CustomAttributeNamedArgument.Argument directly as they are readonly.
The following is equivalent to setting -
[XXX(SomeNamedProperty = {some value})]
var attribDefaultCtorRef = type.Module.Import(typeof(XXXAttribute).GetConstructor(Type.EmptyTypes));
var attrib = new CustomAttribute(attribDefaultCtorRef);
var namedPropertyTypeRef = type.Module.Import(typeof(YYY));
attrib.Properties.Add(new CustomAttributeNamedArgument("SomeNamedProperty", new CustomAttributeArgument(namedPropertyTypeRef, {some value})));
method.CustomAttributes.Add(attrib);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With