Adding Attributes to Member

Topics: Metadata Model, PE reader, PE Writer
Feb 25, 2011 at 5:31 PM

I'm trying to add an attribute to a parameter of a method I've just created. The attribute already exists in another assembly. There's a Parameters property on the MethodDefinition objecct, and on that there's a Attributes property. OK, so I create a new List<ICustomAttribute>...but here's where I get stuck. I want to add a CustomAttribute object to this list, but the Type property is read-only. I'm missing how to get it so it knows to put MyCustomAttribute on the parameter.

Any help is greatly appreciated.



Feb 27, 2011 at 5:49 PM

Never mind! Jason Haley showed me the way. You create a CustomAttribute, then set the Constructor property to the constructor of the custom attribute you want on that member. That does the trick.