Thursday, June 10, 2010

CIL Constant Literals

Sometimes you'll want to create constant literals in CIL code. Most of the time you won't, but when you do, you'll need this. There are multiple ways of representing a constant, such as a constructor argument to a custom attribute, and ildasm.exe will give you something like this:
= ( DE AD BE EF )
If you want to re-write it, you know the signature (it's right next to the literal in the .il file,) so you only need to replace the single encoded binary literal into one like this:
= { string('Hello, World!') bool(true) type(int32) type([System.Xml]System.Xml.XmlDocument) }

1 comment:

Jono said...

This is non-standard according to the ECMA specification for CIL, but it is understood by ILASM.EXE so it's good enough for me (as long as I'm not trying to create a new array in the constructor).