Click here to Skip to main content
13,089,433 members (59,378 online)
Rate this:
Please Sign up or sign in to vote.
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:

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:
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 18-Apr-12 9:27am
Updated 18-Apr-12 9:44am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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:[^]
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I made a few addition to my Arrays and added this code in the codegenerator and it works just fine now :)

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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170813.1 | Last Updated 19 Apr 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100