gpt4 book ai didi

c# - 找不到反序列化 T 类型对象的构造函数

转载 作者:可可西里 更新时间:2023-11-01 12:33:55 24 4
gpt4 key购买 nike

我试图理解 ISerializable 并对此感到困惑。我创建了两个类,它们都具有“Serializable”属性。只有一个类派生自 ISerializable 并且为它定义了 GetObjectData。让我们称这个类为 A。另一个不是从 ISerializable 派生的,GetObjectData 没有为此定义。让我们称这个类为 B。我没有为 A 类提供任何特殊的构造函数。现在在运行时,A 类显示错误,如“缺少特殊构造函数”。这两个类的语法相同。因此,错误可能是其他原因,但不应与构造函数有关。否则我也应该为 B 类得到同样的错误。请参阅下面的代码。谁能说说背后的原因?
注意:我使用的是 windows 7 - 64 位和 visual studio 2010。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace Delete_This
{
[Serializable]
class After_Implementing_ISerializable:ISerializable
{
int a;
string b;
public After_Implementing_ISerializable(int a, string b)
{
this.a = a;
this.b = b;
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{

}
public void Check()
{
After_Implementing_ISerializable s = new After_Implementing_ISerializable(15, "100");
FileStream fs = new FileStream("temp.xml", FileMode.OpenOrCreate);

BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, s);
fs.Close();

fs = new FileStream("temp.xml", FileMode.OpenOrCreate);
After_Implementing_ISerializable d = (After_Implementing_ISerializable)bf.Deserialize(fs);
fs.Close();
}
}

[Serializable]
class Without_Implementing_ISerializable
{
int a;
string b;
public Without_Implementing_ISerializable(int a,string b)
{
this.a = a;
this.b = b;
}

public void Check()
{
Without_Implementing_ISerializable s = new Without_Implementing_ISerializable(15, "100");
FileStream fs = new FileStream("temp.xml", FileMode.OpenOrCreate);

BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, s);
fs.Close();

fs = new FileStream("temp.xml", FileMode.OpenOrCreate);
Without_Implementing_ISerializable d = (Without_Implementing_ISerializable)bf.Deserialize(fs);
fs.Close();
}
}

class Program
{
static void Main(string[] args)
{
Without_Implementing_ISerializable s = new Without_Implementing_ISerializable(5,"Five");
s.Check();

After_Implementing_ISerializable s1 = new After_Implementing_ISerializable(6, "Six");
s1.Check();
}
}
}

这是我得到的错误

"The constructor to deserialize an object of type 'Delete_This.After_Implementing_ISerializable' was not found."}

最佳答案

是的,您需要为实现 ISerializable 的类型实现序列化构造函数;该序列化构造函数负责反序列化对象,就像使用 GetObjectData 方法进行序列化一样。

序列化构造函数看起来像这样,第一个参数 SerializationInfo 和第二个 StreamingContext

protected ClassName(SerializationInfo info, StreamingContext context)
{

}

提供的链接中的备注部分讨论了这个主题。

The ISerializable interface implies a constructor with the signature constructor (SerializationInfo information, StreamingContext context). At deserialization time, the current constructor is called only after the data in the SerializationInfo has been deserialized by the formatter. In general, this constructor should be protected if the class is not sealed.

关于c# - 找不到反序列化 T 类型对象的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22146838/

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