假设我有一个像这样的不可变值类型:
[Serializable]
[DataContract]
public struct MyValueType : ISerializable
{
private readonly int _x;
private readonly int _z;
public MyValueType(int x, int z)
: this()
{
_x = x;
_z = z;
}
// this constructor is used for deserialization
public MyValueType(SerializationInfo info, StreamingContext text)
: this()
{
_x = info.GetInt32("X");
_z = info.GetInt32("Z");
}
[DataMember(Order = 1)]
public int X
{
get { return _x; }
}
[DataMember(Order = 2)]
public int Z
{
get { return _z; }
}
public static bool operator ==(MyValueType a, MyValueType b)
{
return a.Equals(b);
}
public static bool operator !=(MyValueType a, MyValueType b)
{
return !(a == b);
}
public override bool Equals(object other)
{
if (!(other is MyValueType))
{
return false;
}
return Equals((MyValueType)other);
}
public bool Equals(MyValueType other)
{
return X == other.X && Z == other.Z;
}
public override int GetHashCode()
{
unchecked
{
return (X * 397) ^ Z;
}
}
// this method is called during serialization
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("X", X);
info.AddValue("Z", Z);
}
public override string ToString()
{
return string.Format("[{0}, {1}]", X, Z);
}
}
它适用于BinaryFormatter或DataContractSerializer,但是当我尝试将它与protobuf-net一起使用时(http://code.google.com/p/protobuf-net/)序列化器我得到这个错误:
无法对属性应用更改 ConsoleApplication.Program + MyValueType.X
如果我将setter应用于标有DataMember属性的属性,它将起作用,但它会打破这种值类型的不变性,这对我们来说是不可取的。
有谁知道我需要做些什么才能让它发挥作用?我注意到ProtoBu.Serializer.Serialize方法有一个重载,它接受一个SerializationInfo和一个StreamingContext,但是我没有在实现ISerializable接口的上下文之外使用它们,所以任何关于如何使用它们的代码示例这个背景将非常感谢!
谢谢,
编辑:所以我挖了一些旧的MSDN文章,并更好地了解了SerializationInfo和StreamingContext的使用位置和方式,但是当我尝试这样做时:
var serializationInfo = new SerializationInfo(
typeof(MyValueType), new FormatterConverter());
ProtoBuf.Serializer.Serialize(serializationInfo, valueType);
原来那个 Serialize<T>
方法只允许引用类型,有特殊原因吗?考虑到我能够序列化通过引用类型公开的值类型,这似乎有点奇怪。