469,268 Members | 1,021 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,268 developers. It's quick & easy.

Singletons and Serialization.

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;
}
}
Jan 18 '06 #1
7 4703
> 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.
Jan 18 '06 #2
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
Jan 19 '06 #3
Marc Gravell wrote:
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

Jan 19 '06 #4
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.

Cheers for the info,

(the MS article is
http://msdn.microsoft.com/library/de...tdatatopic.asp)

Marc
Jan 19 '06 #5

Marc Gravell wrote:
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

Jan 19 '06 #6
> Marc Gravell wrote:
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


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.
Jan 19 '06 #7
> Marc Gravell wrote:
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


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.
Jan 19 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by stephan beal | last post: by
11 posts views Thread by Tito | last post: by
3 posts views Thread by Dominik Rau | last post: by
6 posts views Thread by Steven Watanabe | last post: by
5 posts views Thread by Omega | last post: by
7 posts views Thread by adam.timberlake | last post: by
12 posts views Thread by Craig Allen | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.