Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用CodeDom初始化数组(或交错数组)?_C#_.net_Codedom - Fatal编程技术网

C# 如何使用CodeDom初始化数组(或交错数组)?

C# 如何使用CodeDom初始化数组(或交错数组)?,c#,.net,codedom,C#,.net,Codedom,我正在尝试使用生成C#(.Net 2.0)代码,该代码将执行以下操作: int[][] myArray = new int[someSize][]; 在CodeDom中,初始化数组需要。MSDN说: 如果语言允许数组的数组,则可以通过在CodeArrayCreateExpression中嵌套CodeArrayCreateExpression来创建它们 据我理解,唯一的可能就是写这样的东西: // Declaration and initialization of myArray Cod

我正在尝试使用生成C#(.Net 2.0)代码,该代码将执行以下操作:

int[][] myArray = new int[someSize][];
在CodeDom中,初始化数组需要。MSDN说:

如果语言允许数组的数组,则可以通过在CodeArrayCreateExpression中嵌套CodeArrayCreateExpression来创建它们

据我理解,唯一的可能就是写这样的东西:

  // Declaration and initialization of myArray
  CodeVariableDeclarationStatement variable =
    new CodeVariableDeclarationStatement("System.Int32[][]", "myArray",
      new CodeArrayCreateExpression("System.Int32[][]",
        new CodeExpression[] { new CodeArrayCreateExpression("System.Int32[]", 0) }));
但这就产生了:

int[][] myArray = new int[][] { new int[0] };
这并不完美,但如果我在生成时知道myArray的大小,我可以使用它,但我不知道

我可以编写一个函数来进行初始化,并在CodeDom中调用它,但如果我可以在纯CodeDom中进行初始化,那就更好了。我错过什么了吗

[编辑]背景信息

其思想是在两个对象表示之间自动生成适配器。我有一个元描述(某种IDL)说:“我有一个容器对象,它有一个int[][]类型的字段”和这个容器的两种表示形式:

// Internal representation
public class InternalContainer {
  int[][] myArray;
}

// Network representation
public class NetworkContainer {
  int[][] myArray;
}
因此,生成可适应任意大小数组的代码的问题。

CodeArrayCreateExpression CodeArrayCreateExpression(数组)
  CodeArrayCreateExpression CodeArrayCreateExpression(Array array)
  {
     CodeArrayCreateExpression arrayCreateExpression = new CodeArrayCreateExpression(array.GetType(), array.GetLength(0));

     if (array.GetType().GetElementType().IsArray)
     {
        CodeArrayCreateExpression[] values = new CodeArrayCreateExpression[array.GetLength(0)];
        for (int j = 0; j < array.GetLength(0); j++)
        {
           values[j] = this.CodeArrayCreateExpression((Array)array.GetValue(j));
        }

        arrayCreateExpression.Initializers.AddRange(values);
     }
     else if(array.GetType().GetElementType().IsPrimitive)
     {
        CodeCastExpression[] values = new CodeCastExpression[array.GetLength(0)];
        for (int j = 0; j < values.Length; j++)
        {
           values[j] = new CodeCastExpression();
           values[j].Expression = new CodePrimitiveExpression(array.GetValue(j));
           values[j].TargetType = new CodeTypeReference(array.GetType().GetElementType());
        }

        arrayCreateExpression.Initializers.AddRange(values);
     }

     return arrayCreateExpression;
  }
{ CodeArrayCreateExpression arrayCreateExpression=新的CodeArrayCreateExpression(array.GetType(),array.GetLength(0)); if(array.GetType().GetElementType().IsArray) { CodeArrayCreateExpression[]值=新的CodeArrayCreateExpression[array.GetLength(0)]; for(int j=0;j
要创建具有动态长度的锯齿阵列,您有以下解决方法:

创建与

ELEMENTTYPE[] array = (ELEMENTTYPE[])Array.CreateInstance(typeof(ELEMENTTYPE), length);

ELEMENTTYPE
可以是任何类型,无论是数组还是非数组。

这是我的解决方案,使用CodeSnippet表达式

public static DOM.CodeExpression NewArray (this Type type, int dim, int size) {
  string dims = String.Concat(Enumerable.Repeat("[]", dim - 1).ToArray());
  return new DOM.CodeSnippetExpression(string.Format("new {0}[{1}]{2}", type.FullName, size, dims));
}

但我需要知道数组的第一维,对吗?我不能,看我的编辑。无论如何,感谢您的一般案例实施。