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

发布于 2024-10-31 08:55:47 字数 1448 浏览 1 评论 0原文

我有一个 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 生成的类。我真的只想 重复 N 次。

I have an XSD.exe generated class. I am using a crude inversion of control. My test object worked until I added this fancy XML snippet:

  <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>

Now, GeneratedByXsdClass object creation is busted.

    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 = ""
                }
            }
        }
    };

Compiler Error:

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

I'm open to modifying the xsd.exe generated class if that makes life easier. I really just want <item> to repeat N times.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失去的东西太少 2024-11-07 08:55:47

将您的示例简化为:

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 = ""
        }
    }
};

Simplify your example down to this:

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

Now you should be able to see what the problem is; you're trying to put an int[][] inside an int[][], and the compiler is saying it expected a int[]. Remove the line with the comment:

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

This is the equivalent in your code:

GeneratedByXsdClass.items = new itemOrderItemsItem[][]{
    new itemOrderItemsItem[]
    {
        new itemOrderItemsItem()
        {
            model = "1000",
            description = "Torque Wrench",
            material = "10002525",
            lot = "3",
            serial = "",
            transferQty = "1",
            shipQty = ""
        }
    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文