我有这门课:
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace Grouping
{
[Serializable]
public class Group<T> : HashSet<T>
{
public Group(string name)
{
this.name = name;
}
protected Group(){}
protected Group(SerializationInfo info, StreamingContext context):base(info,context)
{
name = info.GetString("koosnaampje");
}
public override void GetObjectData(SerializationInfo info,StreamingContext context)
{
base.GetObjectData(info,context);
info.AddValue("koosnaampje", Name);
}
private string name;
public string Name
{
get { return name; }
private set { name = value; }
}
}
}
Run Code Online (Sandbox Code Playgroud)
由于它继承自HashSet,它必须实现ISerializable,因此受保护的构造函数和GetObjectData方法.以前我使用BinaryFormatter成功地序列化和反序列化了这个类.
因为我希望能够检查序列化程序生成的输出,我想切换到DataContractSerializer.
我写了这个测试:
[TestMethod]
public void SerializeTest()
{
var group …Run Code Online (Sandbox Code Playgroud)