[英]Where best store temporary deserialization data?
Can I avoid adding additional fields to a class to store data which is needed only for deserialization/serialization? 我是否可以避免向类中添加其他字段来存储仅用于反序列化/序列化所需的数据?
Suppose I have some class: 假设我有一些课程:
[Serializable]
class MyClass {
[NonSerialized]
NonSerializableDataType myField;
SomeOtherDataType serializableTemporaryData;
[OnSerializing]
OnSerializing (StreamingContext context) {
// build serializableTemporaryData from myField
}
[OnDeserialized]
void OnDeserialized (StreamingContext context) {
// build myField from serializableTemporaryData
}
}
Is there any way to avoid having the serializableTemporaryData
field in each object of MyClass
? 有没有办法避免在
MyClass
每个对象中都有serializableTemporaryData
字段? Can I, for example, make it static (possibly by changing my On... methods)? 例如,我可以将其设置为静态(可能通过更改我的On ...方法)吗?
Constraint: I cannot change the implementation of NonSerializableDataType
. 约束:我无法更改
NonSerializableDataType
的实现。
Example: Suppose myField contains a handle to a resource. 示例:假设myField包含资源的句柄。 Then, on serialization, I must store some information on how to acquire the resource after de-serialization, but I cannot store the handle itself.
然后,在序列化时,我必须存储一些有关如何在反序列化后获取资源的信息,但我无法存储句柄本身。 If I wrap the handle into another class, then I have just shifted the problem to the wrapper class -- I would ask the very same question for the wrapper class then.
如果我将句柄包装到另一个类中,那么我刚刚将问题转移到了包装器类 - 那么我会问包装类的相同问题。
If you need to control the serialization process, you should implement the ISerialization
interface. 如果需要控制序列化过程,则应实现
ISerialization
接口。
[Serializable]
public class MyClass: ISerializable
{
// As you are in control of serialization process now
// [Serialized] and [NonSerialized] attributes are no longer required
private NonSerializableDataType myField;
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
// Create and populate your SomeOtherDataType local variable here, then push it into info variable
// Or even better, dont create SomeOtherDataType, just put additional serialization data into info variable, for example:
info.AddValue("URL", "http://this.way.com");
}
protected MyClass(SerializationInfo info, StreamingContext context)
{
// Dont forget to define constructor for deserialization purpose
this.myField = new NonSerializableDataType(loadFromURL: (string)info.GetValue("URL", typeof(string)));
}
}
No additional class just for serialization data, no fields pollution. 没有额外的类只是为了序列化数据,没有字段污染。 The only potential problem is to watch for any serializable data derived from this class (overwrite
GetObjectData(...)
if needed). 唯一可能的问题是监视从此类派生的任何可序列化数据
GetObjectData(...)
如果需要,覆盖GetObjectData(...)
)。
More info: MSDN ISerializable 更多信息: MSDN ISerializable
There is a concept of serialization surrogates which might help you to overcome the difficulty with SomeNonSerializableType
, since it was designed exactly for this reason.. However, this requires that you have the possibility to configure the serialization surrogates, when the formatter is created. 有一个序列化代理的概念可以帮助您克服
SomeNonSerializableType
的困难,因为它的设计正是出于这个原因。但是,这要求您可以在创建格式化程序时配置序列化代理。 Otherwise, you will need to customize the serialization process by implementing ISerializable
. 否则,您将需要通过实现
ISerializable
来自定义序列化过程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.