gpt4 book ai didi

c# - 序列化 : Cannot convert type string[][] to string[]

转载 作者:行者123 更新时间:2023-11-30 22:55:04 25 4
gpt4 key购买 nike

我有一个带有锯齿状数组的类。当我尝试序列化它时,出现以下异常:

System.InvalidOperationException HResult=0x80131509 Message=Unable to generate a temporary class (result=1). error CS0030: Cannot convert type 'string[][]' to 'string[]' error CS0029: Cannot implicitly convert type 'string[]' to 'string[][]'

重现问题的简单程序:

using System.IO;
using System.Xml.Serialization;

namespace JaggedArraySerialization
{
class Program
{
static void Main(string[] args)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Class1));
var class1 = new Class1();
using (TextWriter stream = new StreamWriter(@"C:\temp\test.xml"))
{
xmlSerializer.Serialize(stream, class1);
}
}
}
}

和类Class1

using System;
using System.Xml.Serialization;

namespace JaggedArraySerialization
{
[Serializable]
public class Class1
{
[XmlElement]
public string[][] MyJaggedArray { get; set; }
}
}

如何序列化锯齿状数组?

最佳答案

您可以在属性 MyJaggedArray 上指定类型,如下所示:

    [Serializable]
public class Class1
{
[XmlElement(Type = typeof(string[][]))]
public string[][] MyJaggedArray { get; set; }
}

关于c# - 序列化 : Cannot convert type string[][] to string[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55648996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com