Click here to Skip to main content
11,718,289 members (87,651 online)
Rate this: bad
good
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 Frown | :(

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 Smile | :) thanks - Jackie
Posted 18-Apr-12 9:27am
Edited 18-Apr-12 9:44am
v3
Rate this: bad
good
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: http://msdn.microsoft.com/en-us/library/2s05feca.aspx[^]
  Permalink  
Rate this: bad
good
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 Smile | :)

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

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

  Print Answers RSS
0 OriginalGriff 215
1 F-ES Sitecore 210
2 Maciej Los 140
3 Jochen Arndt 120
4 Andy Lanng 115
0 OriginalGriff 535
1 Maciej Los 529
2 Richard MacCutchan 440
3 CHill60 415
4 F-ES Sitecore 415


Advertise | Privacy | Mobile
Web01 | 2.8.150901.1 | Last Updated 19 Apr 2012
Copyright © CodeProject, 1999-2015
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