More on Emitting Multi Dimentional Arrays

time to read 10 min | 1820 words

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();

       }

}