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:
Then you can simply populate the custom attributes with string arguments: