数据契约 +多维数组——有什么解决方案吗?

发布于 2024-12-13 03:02:28 字数 354 浏览 0 评论 0原文

来自 MSDN

组合集合类型(具有集合的集合)是 允许。锯齿状数组被视为集合的集合。 不支持多维数组。

那么,如果您无法正常序列化多维数组,如何有效地解决这个问题呢?我的想法是拥有一个属性来展平数组并序列化该集合并在反序列化期间将其展平,但我不确定这是否有效?

以前有人找到解决方案吗?


我应该注意到,我认为展平可能起作用的原因是因为我的尺寸是固定值(它们是硬编码的)。

From MSDN:

Combining collection types (having collections of collections) is
allowed. Jagged arrays are treated as collections of collections.
Multidimensional arrays are not supported.

So, if you can't normally serialize a multidimensional array, how does one get around this efficiently? My thought is to have a property that flattens the array and serialize that collection and unflatten it during deserialization, but I'm not sure if that's efficient?

Anyone find a solution this before?


I should note that the reason I think flattening might work is because my dimensions are a fixed value (they are hard-coded).

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

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

发布评论

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

评论(3

半枫 2024-12-20 03:02:28

是的,您可以将其压平或锯齿化,无论哪种更方便。

Yes, you could flatten it or Jagged-ize it, whatever is more convenient.

像你 2024-12-20 03:02:28

每当你遍历一个二维数组时,你都会花费 O(n) 的努力(或者输入中的每个项目一个“处理”单元)。正如您所说,在二维和一维之间进行转换并不麻烦。除非您处理的数据量非常大(Web 服务的调用频率的数组大小),或者在高度受限的系统(.Net Compact 或 .Net Micro Frameworks)上,否则我怀疑这确实是一个大问题。像排序这样的事情会变得昂贵。

       string[,] input = new string[5, 3];
       string[] output = new string[15];

       for (int i = 0; i < input.GetUpperBound(0); i++)
       {
           for (int j = 0; j < input.GetUpperBound(1); j++)
           {
               output[j * input.GetUpperBound(j) + i] = input[i, j];
           }
       }

Any time you rip through a 2-D array you're expending O(n) effort (or one "processing" unit per item in the input). It's not much trouble to convert between 2-D and 1-D and back as you said. Unless you're dealing in really high volumes (array size of call frequency of the web service), or on a highly constrained system (.Net Compact or .Net Micro Frameworks), I doubt this would really be a big issue. Things like sorting are what get expensive.

       string[,] input = new string[5, 3];
       string[] output = new string[15];

       for (int i = 0; i < input.GetUpperBound(0); i++)
       {
           for (int j = 0; j < input.GetUpperBound(1); j++)
           {
               output[j * input.GetUpperBound(j) + i] = input[i, j];
           }
       }
笨死的猪 2024-12-20 03:02:28

也许有一个扩展方法:

public static string[][] Jaggedize(this string[,] input) {
  string[][] output = new string[input.GetLength(0)][];
  for (int i = 0; i < input.GetLength(0); i++) {
    output[i] = new string[input.GetLength(1)];
    for (int j = 0; j < input.GetLength(1); j++) {
      output[i][j] = input[i, j];
    }
  }
  return output;
}

Maybe with an extension method:

public static string[][] Jaggedize(this string[,] input) {
  string[][] output = new string[input.GetLength(0)][];
  for (int i = 0; i < input.GetLength(0); i++) {
    output[i] = new string[input.GetLength(1)];
    for (int j = 0; j < input.GetLength(1); j++) {
      output[i][j] = input[i, j];
    }
  }
  return output;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文