tags:

views:

41

answers:

1

What is the syntax for defining an array literal in CIL for the purposes of decorating a member with a custom attribute?

I am writing some .NET code in CIL (using ilasm.exe to compile it) and I need to decorate a method with a custom attribute. The constructor for that attribute takes an array of integers as its only parameter. How can I do this in CIL?

This is the signature of the custom attribute's constructor (I can't change it):

public FooAttribute(int[] values) {
// some hidden constructor stuff
}

This is how I'd decorate my method if I were writing in C# (but I can't):


[Foo(new int[] {1, 2, 3, 4})]
public string Bar() {
  return "Some text";
}

Using ildasm.exe to look at the compiled C# (to try and understand by reverse engineering) gives me an ugly and unusable binary literal. I tried using Reflector.NET instead and it looks much better but ilasm.exe throws a syntax error at the keyword "new" so I can't use it:

.custom instance void SomeNamespace.FooAttribute::.ctor(int32[]) = { new int32[int32(4)] { int32(1), int32(2), int32(3), int32(4) } }
+2  A: 

Hard to guess what your problem might be. If I apply this attribute to a Program.Test() method, I get this:

  .method private hidebysig static void  Test() cil managed
  {
    .custom instance void ConsoleApplication1.FooAttribute::.ctor(int32[]) = ( 01 00 04 00 00 00 01 00 00 00 02 00 00 00 03 00 
                                                                               00 00 04 00 00 00 00 00 ) 
    // Code size       2 (0x2)
    .maxstack  8
    IL_0000:  nop
    IL_0001:  ret
  } // end of method Program::Test

Run this through ilasm.exe, no problem. Note how the array element values (scroll the snippet window to the right to see them) are already converted into the format needed to embed them in the attribute constructor data table. BitConverter.GetBytes() can get part of that job done. The Ecma document should have the required format of that data.

Hans Passant
The output from ildasm (as you've shown above) has the array already converted into the the correct format, but this is not required and for other types/arguments it is possible to supply them in the format: <code>= { string('Hello, World!') bool(true) type(int32) type([System.Xml]System.Xml.XmlDocument) }</code> rather than being forced to pre-format them.
Jono
I can't read that. Ecma 335, partition 2, chapter 21 says you can't. Ctor data must be in bytes. Which perhaps explains why you have trouble making something else work? Don't assume the format shown by a disassembler follows the standard.
Hans Passant
Indeed, the ECMA standard says nothing about the simple format I was using for contructor arguments. ilasm.exe has no problem with several of the basic types (strings, ints, bools, types, etc) but it would appear they are non-standard.
Jono