Serializing a Singleton

  • Thread starter Thread starter Joe Tavares
  • Start date Start date
J

Joe Tavares

I have read several articles on serializing a singleton and have attempted
to implement it in my code. Unfortunately, the object is not being
serialized. The file size on disk is 1K. I am not sure exactly what I am
missing here, do I need to use AddValue to add the object fields I want to
serialize? None of the examples showed this. I can only assume it is
something basic since the overall pattern appears quite simple. I have
included a code snippet below which shows the pertinent parts.

Any help that anyone can supply would be very appreciated.

-joe-


[Serializable]
sealed public class Singleton: ISerializable
{
public static readonly Singleton Instance =
new Singleton ();


private List<cdmPendingCompoundObject> p_PendingCompoundObjects =
new List<cdmPendingCompoundObject>();
private String []p_FieldMaps;
private String[] p_DelimitedFields;
private String p_MetadataTitleNickName = "title";

void ISerializable.GetObjectData(
SerializationInfo info, StreamingContext context)
{
info.SetType(typeof(SingletonSerializationHelper));
}
}

[Serializable]
internal class SingletonSerializationHelper : IObjectReference
{
public Object GetRealObject(StreamingContext context)
{
return Singleton.Instance;
}

}
 
Joe,

If you are implementing ISerializable, then you have to have a
constructor that takes a SerializationInfo instance as well as a
StreamingContext value. Then you would call AddValue, passing the values to
the SerializationInfo instance to be persisted.

However, looking at your instance, it doesn't seem like you will have to
implement ISerializable. Assuming that the type cdmPendingCompoundObject is
serializable, you can just place the Serializable attribute on the Singleton
type and you will be able to serialize those instances.

However, I would say that allowing the type to be publically
serializable isn't a good idea, because you could then create other
instances of what is supposed to be a singleton object.

I would recommend creating a private nested class for your singleton
that represents what is persisted (or something internal to the assembly
that the singleton is in, at least) and then serializing that, then
populating the singleton instance when you deserialize the instance.
 
Nicholas,
Perhaps I am still missing something?

The reason I implemented ISeriazable was to get around the problem that you
elluded to below. By implementing the ISerializable and substituting a helper
class I should be able to get around the problem of deserialization creating
multiple instances of the class. There are a few different examples out
there the one I like best is
http://dotnetslackers.com/community...ot-a-singleton_3F00_-Serialization_2100_.aspx.

In these examples I do not see AddValue being used, however, it may be
because they are trying to create a simple example and do not want to confuse
the issue. In fact in the example on MSDN it explicitly states that the
constructor is not needed
(http://msdn2.microsoft.com/en-us/library/system.runtime.serialization.serializationinfo.aspx)

Perhaps, this just demonstrates poor knowledge of how custom serialization
works and I do need the AddValue. I think I will give that a try.

-joe-

Nicholas Paldino said:
Joe,

If you are implementing ISerializable, then you have to have a
constructor that takes a SerializationInfo instance as well as a
StreamingContext value. Then you would call AddValue, passing the values to
the SerializationInfo instance to be persisted.

However, looking at your instance, it doesn't seem like you will have to
implement ISerializable. Assuming that the type cdmPendingCompoundObject is
serializable, you can just place the Serializable attribute on the Singleton
type and you will be able to serialize those instances.

However, I would say that allowing the type to be publically
serializable isn't a good idea, because you could then create other
instances of what is supposed to be a singleton object.

I would recommend creating a private nested class for your singleton
that represents what is persisted (or something internal to the assembly
that the singleton is in, at least) and then serializing that, then
populating the singleton instance when you deserialize the instance.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Joe Tavares said:
I have read several articles on serializing a singleton and have attempted
to implement it in my code. Unfortunately, the object is not being
serialized. The file size on disk is 1K. I am not sure exactly what I am
missing here, do I need to use AddValue to add the object fields I want to
serialize? None of the examples showed this. I can only assume it is
something basic since the overall pattern appears quite simple. I have
included a code snippet below which shows the pertinent parts.

Any help that anyone can supply would be very appreciated.

-joe-


[Serializable]
sealed public class Singleton: ISerializable
{
public static readonly Singleton Instance =
new Singleton ();


private List<cdmPendingCompoundObject> p_PendingCompoundObjects =
new List<cdmPendingCompoundObject>();
private String []p_FieldMaps;
private String[] p_DelimitedFields;
private String p_MetadataTitleNickName = "title";

void ISerializable.GetObjectData(
SerializationInfo info, StreamingContext context)
{
info.SetType(typeof(SingletonSerializationHelper));
}
}

[Serializable]
internal class SingletonSerializationHelper : IObjectReference
{
public Object GetRealObject(StreamingContext context)
{
return Singleton.Instance;
}

}
 
Back
Top