Ayende @ Rahien

Unnatural acts on source code

More on Emitting Multi Dimentional Arrays

Here is a small code sample that demonstrate the problem. This doesn't work, but changing the Run method parameter to anything but a multi dimentional array does work.

public interface WithMatrix

{

       void Run(int[,] matrix);

}

 

class Program

{

       static void Main(string[] args)

       {

              AssemblyName assemblyName = new AssemblyName("test");

              AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain

                     .DefineDynamicAssembly(assemblyName,AssemblyBuilderAccess.RunAndSave);

              ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("myModule");

              TypeBuilder typeBuilder = moduleBuilder.DefineType("Neo", TypeAttributes.Class);

              typeBuilder.AddInterfaceImplementation(typeof(WithMatrix));

              MethodBuilder methodBuilder = typeBuilder.DefineMethod("Run",

                         MethodAttributes.Public | MethodAttributes.Virtual,typeof(void), new Type[]{typeof(int[,])});

              methodBuilder.GetILGenerator().Emit(OpCodes.Ret);

              Type type = typeBuilder.CreateType();

       }

}

Comments

Tomer Gabel
02/17/2007 06:07 PM by
Tomer Gabel

I think Microsoft actually fucked up on this. I played around with it and there doesn't seem to be any way to define a method other than TypeBuilder.DefineMethod, which requires you to define everything manually.

I think a much more reasonable solution would be an overload of DefineMethod which accepts a MethodInfo instance as prototype. It would potentially save heaps of bug, even if we ignore this particular (esoteric) bug in the class library.

Anyway I suggest you open a bug report on this.

Jb Evain
02/17/2007 08:37 PM by
Jb Evain

That works perfectly tho:

http://monoport.com/1811

Ayende Rahien
02/17/2007 11:12 PM by
Ayende Rahien

While this is helpful, I have an existing code base that uses Reflection Emit, and it is not practical to move it to Cecil.

Comments have been closed on this topic.