Reputation: 33098
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?
Upvotes: 3
Views: 412
Reputation: 141638
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:
internal class MyAttribute : Attribute
{
public string Foo;
}
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:
.custom instance void Dummy.MyAttribute::.ctor() = ( 01 00 01 00 53 0E 03 46 6F 6F 01 34 )
These values mean:
Upvotes: 5
Reputation: 1910
The encoding is documented in "MS Partition II" section 23.3, which can be downloaded @
re: http://msdn.microsoft.com/en-us/netframework/Aa569283.aspx
Upvotes: 2
Reputation: 2886
It will allow precompiling attributes and just geschmack them into the binary instead of going thro the builder.
Upvotes: 1