I'm trying to use CodeDom to generate C# (.Net 2.0) code that would do the following:
int[][] myArray = new int[someSize][];
In CodeDom, initializing an array requires a CodeArrayCreateExpression. The MSDN says:
If a language allows arrays of arrays, it is possible to create them by nesting a CodeArrayCreateExpression within a CodeArrayCreateExpression.
The way I understand it, the only possibility is to write something like this:
// Declaration and initialization of myArray
CodeVariableDeclarationStatement variable =
new CodeVariableDeclarationStatement("System.Int32[][]", "myArray",
new CodeArrayCreateExpression("System.Int32[][]",
new CodeExpression[] { new CodeArrayCreateExpression("System.Int32[]", 0) }));
But this generates this:
int[][] myArray = new int[][] { new int[0] };
That's not perfect but I could do with it if I knew the size of myArray at generation time, which I don't.
I could write a function that does the initialization and call it in CodeDom but it would be nicer if I could do it in pure CodeDom. Did I miss something ?
[EDIT] Background information
The idea is to automatically generate an adapter between two object representations. I have a meta-description (some kind of IDL) saying: "I have a container object which has a field of type int[][]" and two representations of this container:
// Internal representation
public class InternalContainer {
int[][] myArray;
}
// Network representation
public class NetworkContainer {
int[][] myArray;
}
Thus the question of generating code that can adapt to any size of array.