Click here to Skip to main content
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 10:27am
Edited 18-Apr-12 10: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 525
1 Maciej Los 319
2 BillWoodruff 174
3 Suraj Sahoo | Coding Passion 170
4 /\jmot 152
0 OriginalGriff 8,654
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,229
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 19 Apr 2012
Copyright © CodeProject, 1999-2014
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