为什么我无法在 C# 中序列化元组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13739348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:38:37  来源:igfitidea点击:

Why I could not serialize a tuple in C#?

c#serializationtuples

提问by user1278322

Possible Duplicate:
Why XML-Serializable class need a parameterless constructor

可能的重复:
为什么 XML-Serializable 类需要无参数构造函数

I'm trying to serialize a tuple in my code:

我正在尝试在我的代码中序列化一个元组:

List<List<Tuple<String, CodeExtractor.StatementNode>>> results = null;
results = extractor.ExtractSourceCode(sourceCode);
FileStream fs = new FileStream(@"C:\Projects\Test\ast.xml", FileMode.Create);

XmlSerializer formatter = new XmlSerializer(
    typeof(List<List<Tuple<String, CodeExtractor.StatementNode>>>));

formatter.Serialize(fs, results);

fs.Close();

but it was failed and catch the exception like this:

但它失败了并捕获了这样的异常:

System.Tuple`2[System.String,CodeExtractor.StatementNode] cannot be serialized because it does not have a parameterless constructor.

System.Tuple`2[System.String,CodeExtractor.StatementNode] 无法序列化,因为它没有无参数构造函数。

and I'm do sure the CodeExtractor.StatementNodecould be serialized.

我确信CodeExtractor.StatementNode可以序列化。

回答by Erno

As the exception tells you: The Tuple<T1, T2>has NO parameterless constructor which is needed by the serializer.

正如异常告诉您的那样:Tuple<T1, T2>没有序列化程序所需的无参数构造函数。

回答by Rune FS

For XmlSerializerto be able to do its job it needs a default contructor. That is a constructor that takes no arguments. All the Tuple<...>classes have a single constructor and that constructor takes a number of arguments. One for each value in the tuple. So in your case the sole constructor is

为了XmlSerializer能够完成它的工作,它需要一个默认的构造函数。那是一个没有参数的构造函数。所有的Tuple<...>类都有一个构造函数,并且该构造函数接受许多参数。元组中的每个值一个。所以在你的情况下,唯一的构造函数是

Tuple(T1 value1, T2 value2)

The serializer is looking for a constructor with no arguments and because it can't find it, you get the exception.

序列化程序正在寻找一个没有参数的构造函数,因为它找不到它,所以你得到了异常。

you could create a mutable class, that could be substituted for tuples for the purpose of serialization

您可以创建一个可变类,该类可以替换为序列化的元组

class MyTuple<T1, T2>
{
    MyTuple() { }

    public T1 Item1 { get; set; }
    public T2 Item2 { get; set; }

    public static implicit operator MyTuple<T1, T2>(Tuple<T1, T2> t)
    {
         return new MyTuple<T1, T2>(){
                      Item1 = t.Item1,
                      Item2 = t.Item2
                    };
    }

    public static implicit operator Tuple<T1, T2>(MyTuple<T1, T2> t)
    {
        return Tuple.Create(t.Item1, t.Item2);
    }
}

You could then use it the following way

然后您可以通过以下方式使用它

XmlSerializer formatter = new XmlSerializer(
    typeof(List<List<MyTuple<String, CodeExtractor.StatementNode>>>));

formatter.Serialize(fs, results.SelectMany(
                              lst => lst.Select(
                                        t => (MyTuple)t
                                     ).ToList()
                              ).ToList());