Question

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) } }
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top