使用JSON语法实例化锯齿状数组

时间:2011-04-07 21:52:46

标签: c# xsd.exe

我有一个XSD.exe生成的类。我正在使用粗略的控制反转。我的测试对象一直工作,直到我添加了这个花哨的XML片段:

  <items>
    <item>
      <model>1000</model>
      <description>Torque wrench</description>
      <material>1545454</material>
      <lot>3</lot>
      <serial></serial>
      <transferQty>1</transferQty>
      <shipQty></shipQty>
    </item>
    <item>
         //..item 2
    </item>
    <item>
          //...item 3
    </item>
  </items>
</itemOrder>

现在,GeneratedByXsdClass对象创建被破坏了。

    GeneratedByXsdClass.items = new itemOrderItemsItem[][]{
        new itemOrderItemsItem[1][]//this hardcoded 1 bothers me.  better way?
        {
            new itemOrderItemsItem[]
            {
                new itemOrderItemsItem()
                {
                    model = "1000",
                    description = "Torque Wrench",
                    material = "10002525",
                    lot = "3",
                    serial = "",
                    transferQty = "1",
                    shipQty = ""
                }
            }
        }
    };

编译器错误:

Error   3   Cannot implicitly convert type 'itemOrderItemsItem[][]' to 'itemOrderItemsItem[]'   

我愿意修改xsd.exe生成的类,如果这样可以让生活更轻松。我真的只想让<item>重复N次。

1 个答案:

答案 0 :(得分:0)

简化您的示例:

new int[][]{
    new int[1][]//this hardcoded 1 bothers me.  better way?
    {
        new int[]
        {
            4
        }
    }
}

现在你应该能够看到问题所在;你试图将int[][]置于int[][]内,编译器说它预期为int[]。删除注释中的行:

new int[][]{
    new int[]
    {
        4
    }
}

这与您的代码相同:

GeneratedByXsdClass.items = new itemOrderItemsItem[][]{
    new itemOrderItemsItem[]
    {
        new itemOrderItemsItem()
        {
            model = "1000",
            description = "Torque Wrench",
            material = "10002525",
            lot = "3",
            serial = "",
            transferQty = "1",
            shipQty = ""
        }
    }
};