简体   繁体   English

ISerializable - 序列化单例

[英]ISerializable - Serializing singletons

The ISerializable interface only provides a method to serialize the object. ISerializable接口仅提供序列化对象的方法。 The deserialization process is managed by a constructor. 反序列化过程由构造函数管理。

The problem is, that constructor cannot return an instance, because the constructor CREATES a new instance. 问题是,构造函数无法返回实例,因为构造函数创建了一个新实例。

In my implementation, there are several attributes corresponding to singletons instantiated and maintainded somewhere else. 在我的实现中,有几个属性对应于实例化并在其他地方维护的单例。

I need the deserialization process to get that instance and assign to the attribute, instead of creating a new instance. 我需要反序列化过程来获取该实例并分配给属性,而不是创建新实例。

The constructor approach is not suitable for this. 构造函数方法不适用于此。

In Java, you would call the ReadResolve() method, is there a C# equivalent? 在Java中,您将调用ReadResolve()方法,是否存在C#等价物?

You need to make your classes which save a reference to the singleton implement IObjectReference. 您需要创建保存对单例实现IObjectReference的引用的类。

Take a look at http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iobjectreference.aspx 看看http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iobjectreference.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM