在运行时使用Emit的C#数组 [英] C# array at runtime using emit
问题描述
大家好,我正在C#编译器上工作,到目前为止,我不得不制作数组.现在我已经尝试过opcodes.newarr,并且工作得还不错,但是我的问题是我想以[,]而不是[] []的类型发出一个数组,然后移到opcodes.newobj那里,但是我不知何故无法使其工作:(
原因是我想在newarr上使用newobj是因为我想说8个三维数组(只是被高估了:p),即使您很少会使用3个以上的数组,但我想保证如果有的话可以使它变大数组是可能的.
到目前为止,解析器上的内容是:
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
我也将数组存储在具有ident等的localbuilder中,所以我非常确定它的发射器在...上做错了什么...
如果有人可以帮助我,我会很高兴:)谢谢-Jackie
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
推荐答案
如果我明白你想做什么,我不相信这是可能的.您要执行的操作类似于锯齿状数组: http://msdn.microsoft.com/en-us /library/2s05feca.aspx [ ^ ]
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[^]
我在数组中添加了一些内容,并将此代码添加到了代码生成器中,现在可以正常使用了:)
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);
这篇关于在运行时使用Emit的C#数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!