Singleton Serialization

One of the techniques that I’ve found to be of some value is the usage of class instances as enums. An example of this is…


public class MonitorType
{
    public static readonly class BenQMonitor = new        MonitorType("BenQ");
    public static readonly class SamsungMonitor = new        MonitorType("Samsung");
...

One thing I haven’t had to deal with yet was serializing one of these.

If you think about it though this would cause a problem though since de-serialization would cause a new instance to be created, when really you want your singleton instance. But thankfully this little problem has been solved via this little article. Good thing to keep in the back of your brain if you follow this enum approach (or any other approach where you would serialize a singleton.).

Advertisements
Singleton Serialization

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s