Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
Hi all im working on a C# compiler and i've reached this far that i have to make arrays. now i've tried opcodes.newarr and that works somewhat fine but my problem is that i would like to emit a array at type [,] and not[][] and have there by moved on to opcodes.newobj but somehow i cant make it work :(

The reason is i wanna use newobj over newarr is that i wanna make lets say 8 dimentional arrays (just to be over rated :p) even that you rarely would use more than 3 but i wanna be safe that if some ever would make such big array it is possible.

what i have so far is on the parser is:

C#
Arrays a = new Arrays(); //Arrays is a class with "Array" in it
a.array = Array.CreateInstance(value, dimensionsize.ToArray()); //value is a system.type and dimensiomsize is a list where all int array leght is so if it have 4 values and value is of type int the array type is int[,,,]

And the codegenerator:
C#
il.Emit(OpCodes.Newobj, typeof(array)); //the "array" is the stored declared array from above


i've also stored the array in a localbuilder with ident etc. so im very sure its the emiter im doing something wrong at...

If someone could help me out i would be happy :) thanks - Jackie
Posted
Updated 18-Apr-12 9:44am
v3

If I understand what you want to do, I do not beleive it is possible. What you want to do is similar to jagged arrays: http://msdn.microsoft.com/en-us/library/2s05feca.aspx[^]
 
Share this answer
 
I made a few addition to my Arrays and added this code in the codegenerator and it works just fine now :)

C#
Arrays arr = (Arrays)value;
deliveredType = arr.array.GetType();
Type myType = arr.array.GetType();
Type[] types = new Type[arr.dimensions.Length];
for (int i = 0; i < arr.dimensions.Length; i++)
{
    types[i] = arr.type;
    il.Emit(OpCodes.Ldc_I4, arr.dimensions[i]);
}

ConstructorInfo constructorInfoObj = myType.GetConstructor(types);


il.Emit(OpCodes.Newobj, constructorInfoObj);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900