I can't figure how to add custom attribute to a method using Mono.Cecil The attributes that I would want to add is like this :
.custom instance void [mscorlib]System.Diagnostics.DebuggerHiddenAttribute::.ctor() = ( 01 00 00 00 )
Does anyone know how to add custom attributes
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 ofbrowsable
parameter to its constructor:Also, named arguments can be added to
Properties
collection of the createdCustomAttribute
.It's actually very easy.
This is my take,
I noticed Jb Evain's snippet is slightly different. I'm not sure whether that is because is because he's using a newer version of Cecil or because I'm wrong :)
In my version of Cecil,
Import
returns aTypeReference
, not the constructor.