使用 DataContractSerializer 进行自定义序列化
我目前正在为我的数据集使用包装类,以实现自定义序列化。我想使用 DataContractSerializer (更像是必须使用它)但仍然支持自定义序列化。问题是 [DataContract]
和 [Serializing]
属性似乎相处得不太好......我如何覆盖序列化,并支持 两者 DataContract 和ISerialized序列化? 包装器 DataSet 类的代码位于此处:
[Serializable()]
[System.Runtime.InteropServices.ComVisible(false)]
public class TestDatasetWrapper : TestDataSet, ISerializable
{
public TestDatasetWrapper()
: base()
{}
protected TestDatasetWrapper(SerializationInfo info, StreamingContext context)
{
SerializationHelper.DeserializeTypedDataSet(info, this);
}
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
SerializationHelper.AddTypedDataSetObjectData(info, this);
}
}
谢谢!
I'm currently using wrapper classes for my DataSets ,in order to implement custom serialization. I would like to use DataContractSerializer
(more like have to use it) but still support the custom serialization. The problem is that the [DataContract]
and [Serializable]
attributes don't seem to get along so well... how could I override the serialization, and support BOTH DataContract & ISerializable serialization?
The code for the wrapper DataSet class is brought here:
[Serializable()]
[System.Runtime.InteropServices.ComVisible(false)]
public class TestDatasetWrapper : TestDataSet, ISerializable
{
public TestDatasetWrapper()
: base()
{}
protected TestDatasetWrapper(SerializationInfo info, StreamingContext context)
{
SerializationHelper.DeserializeTypedDataSet(info, this);
}
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
SerializationHelper.AddTypedDataSetObjectData(info, this);
}
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DataContractAttribute 和 SerializedAttribute 可以一起使用。这里的好处是,您也不需要使用单独的序列化器。 DataContractSerialzer 是一个 XmlObjectSerializer,它本身支持 [Serialized]。例如:
OUTPUT:“Matt”
仅使用 SerializedAttribute 属性,我们就可以使用 DataContractSerializer 成功序列化和反序列化对象...
使用 ISerialized,我们可以做同样的事情:
OUTPUT< /strong>: "MATT"
并且使用 DataContractAttribute:
OUTPUT: "Matt"
OUTPUT: 0
当 DataContractSerializer 遇到具有 DataContractAttribute 的类型时,它将使用该类型而不是将序列化传递给其基类型,该基类型处理 SerializedAttribute 和 ISerialized 接口。
如果您遇到问题,是序列化问题,还是反序列化问题,还是两者兼而有之?
The DataContractAttribute and the SerializableAttribute can both be used together. The bonus here is, you don't need to use seperate serialisers either. The DataContractSerialzer is an XmlObjectSerializer, which itself supports [Serializable]. For instance:
OUTPUT: "Matt"
Using a just a SerializableAttribute attribute we can successfully serialise and deserialise an object using the DataContractSerializer...
Using ISerializable, we can do the same thing:
OUTPUT: "MATT"
And with a DataContractAttribute:
OUTPUT: "Matt"
OUTPUT: 0
When the DataContractSerializer encounters a type with a DataContractAttribute, it will use that instead of passing serialization to its base type, which handles the SerializableAttribute and ISerializable interfaces.
If you are encountering issues, is it with serialisation, or with deserialisation, or both?