Singletons and Serialization.

C

chris martin

I need to serialize a singelton in my system.

It is possible to serialize with BinaryFormatter or SoapFormatter using ISerializable
and a helper IObjectReference class. But, what I'm after is plain-jane XmlSerializer
support.

Is there any way this is possible?

Chris

---
using System;
using System.Runtime.Serialization;

[Serializable]
public sealed class TestSingleton : ISerializable
{
private TestSingleton()
{}

public static TestSingleton Instance
{
get { return Creator.instance; }
}

private class Creator
{
internal static readonly TestSingleton instance = new TestSingleton();
}

public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.SetType(typeof(SerializationHelper));
}
}

[Serializable]
public sealed class SerializationHelper : IObjectReference
{
public object GetRealObject(StreamingContext context)
{
return TestSingleton.Instance;
}
 
C

chris martin

I need to serialize a singelton in my system.
It is possible to serialize with BinaryFormatter or SoapFormatter
using ISerializable and a helper IObjectReference class. But, what I'm
after is plain-jane XmlSerializer support.

Is there any way this is possible?

Chris

---
using System;
using System.Runtime.Serialization;
[Serializable]
public sealed class TestSingleton : ISerializable
{
private TestSingleton()
{}
public static TestSingleton Instance
{
get { return Creator.instance; }
}
private class Creator
{
internal static readonly TestSingleton instance = new
TestSingleton();
}
public void GetObjectData(SerializationInfo info, StreamingContext
context)
{
info.SetType(typeof(SerializationHelper));
}
}
[Serializable]
public sealed class SerializationHelper : IObjectReference
{
public object GetRealObject(StreamingContext context)
{
return TestSingleton.Instance;
}
}

BTW. The XmlSerializer barfs, of course, on TestSingleton not having a public
ctor
 
M

Marc Gravell

Well, if you can serialize it, then presumably you want to deserialize it at
some point... (unless this is write-only for audit purposes or some-such).
If you deserialize something it creates an instance of an object. Forgetting
about the private / public issues, this means you could have more than one
object (I can deserialize the same piece of XML as many times as I like),
hence you no longer have a singleton...

So if (by the above) you are already prepared for the object to not
*actually* be a singleton, you may as well have a public ctor, a static
instance, and just document that they should be using *that one*.

So ; no: I don't think you can do this using the in-build xml-serialization.

Marc
 
J

Jon Skeet [C# MVP]

Marc said:
Well, if you can serialize it, then presumably you want to deserialize it at
some point... (unless this is write-only for audit purposes or some-such).
If you deserialize something it creates an instance of an object.

I'm not sure that's true. As it happens, the documentation for
ISerializable.GetObjectData (at least for 1.1) gives an example which
just happens to be "how to serialize/deserialize a singleton". With any
luck, that's exactly what's required here...

Jon
 
J

Jon Skeet [C# MVP]

Marc said:
Fair comment - and an interesting example; you squarely countered my
singleton point.

I might be wrong (again), but I didn't think that the xml-serializer used
this interface? However, the concept could probably be twisted to suit the
purpose.

Pass, I'm afraid. Serialization is one of those topics I've mostly
avoided... it was pure fluke that MSDN happened to have the relevant
info this time :)

Jon
 
C

chris martin

Marc said:
I'm not sure that's true. As it happens, the documentation for
ISerializable.GetObjectData (at least for 1.1) gives an example which
just happens to be "how to serialize/deserialize a singleton". With
any luck, that's exactly what's required here...

Jon

I've done the testing of deserialization to a singleton both with and without
an IObjectReference.GetRealObject call. Without the call, the singleton object
is no longer a singleton. But, with the call it is.
 
C

chris martin

Marc said:
Pass, I'm afraid. Serialization is one of those topics I've mostly
avoided... it was pure fluke that MSDN happened to have the relevant
info this time :)

Jon

Well. I can't find a way to use XmlSerializer with a singleton. I'll have
to restrict clients to use Binary or Soap somehow. Should be easy enough.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top