Are there any meaningful ways to use
public void SetCustomAttribute(
ConstructorInfo con,
byte[] binaryAttribute
)
from AssemblyBuilder.SetCustomAttribute
Clarification: by meaningful, I mean what kind of data would you ever want to fill that array with? What does it do with it?
Is there something like I could binary formatter serialize an existing Attribute and pass it that data?
Because attribute storage is a magic science. They are always stored using a binary format that is encoded, and in this case it’s just giving you raw write-access to the binary content of the attribute. Largely, this would be used by someone implementing their own compiler, or are trying to squeak out every bit of performance as possible. You can’t just put anything you’d like in there, you are expected to follow the CLI’s specifications.
For example, the value of the constructor arguments in an attribute must be encoded. Take for example the following attribute:
And if we apply it with something like
[MyAttribute(Foo = "4")]to a token, it must serialize:So it gets compiled into something like this:
These values mean: