By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,694 Members | 1,310 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,694 IT Pros & Developers. It's quick & easy.

Singleton Pattern & Serialization

P: n/a
Hi,

How would I make a class implementing a Singleton Pattern serializable.
My problem is the Singleton Pattern 'requires' the constructor to be Private
and serialization requires the constructor to be Public.

Thanks,

Michael
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
You can make the constructor protected:
Imports System.Runtime.Serialization

....

Protected Sub New(ByVal info As SerializationInfo, ByVal context As
StreamingContext)
MyBase.New(info, context)
End Sub


Shariq
sh****@shariqkhan.com
"Michael Maes" <mi*****@merlot.com> wrote in message
news:69**********************************@microsof t.com...
Hi,

How would I make a class implementing a Singleton Pattern serializable.
My problem is the Singleton Pattern 'requires' the constructor to be
Private
and serialization requires the constructor to be Public.

Thanks,

Michael

Nov 21 '05 #2

P: n/a
Of course,

how silly of me not to think about that!

Thanks a 1.000 times Shariq.

Kind regards,

Michael

"Shariq Khan" wrote:
You can make the constructor protected:
Imports System.Runtime.Serialization

....

Protected Sub New(ByVal info As SerializationInfo, ByVal context As
StreamingContext)
MyBase.New(info, context)
End Sub


Shariq
sh****@shariqkhan.com
"Michael Maes" <mi*****@merlot.com> wrote in message
news:69**********************************@microsof t.com...
Hi,

How would I make a class implementing a Singleton Pattern serializable.
My problem is the Singleton Pattern 'requires' the constructor to be
Private
and serialization requires the constructor to be Public.

Thanks,

Michael


Nov 21 '05 #3

P: n/a
Michael,
See the second article of the following 3 part series on Serialization on
how to serialize & deserialize a Singleton. You need to create a
Serialization Helper class that implements IObjectRereference. The class
being serialized needs to use SerializationInfo.SetType to indicate that
this helper class is being written instead of the actual class. The helper
class's GetRealObject method returns the instance of the singleton....

The following three part article covers binary serialization in detail:
http://msdn.microsoft.com/msdnmag/issues/02/04/net/
http://msdn.microsoft.com/msdnmag/issues/02/07/net/
http://msdn.microsoft.com/msdnmag/issues/02/09/net/

Hope this helps
Jay
"Michael Maes" <mi*****@merlot.com> wrote in message
news:69**********************************@microsof t.com...
| Hi,
|
| How would I make a class implementing a Singleton Pattern serializable.
| My problem is the Singleton Pattern 'requires' the constructor to be
Private
| and serialization requires the constructor to be Public.
|
| Thanks,
|
| Michael
Nov 21 '05 #4

P: n/a
Hi Jay,

Thanks for the links.
I am indeed still facing issues re the Scope of the Constructor.
I think me problem lies in the fact that the Classes Inherit from a
Base-Class and use an XmlSerializer.

I'm going to try out the approach of the article (today).

Thanks,

Michael

"Jay B. Harlow [MVP - Outlook]" wrote:
Michael,
See the second article of the following 3 part series on Serialization on
how to serialize & deserialize a Singleton. You need to create a
Serialization Helper class that implements IObjectRereference. The class
being serialized needs to use SerializationInfo.SetType to indicate that
this helper class is being written instead of the actual class. The helper
class's GetRealObject method returns the instance of the singleton....

The following three part article covers binary serialization in detail:
http://msdn.microsoft.com/msdnmag/issues/02/04/net/
http://msdn.microsoft.com/msdnmag/issues/02/07/net/
http://msdn.microsoft.com/msdnmag/issues/02/09/net/

Hope this helps
Jay
"Michael Maes" <mi*****@merlot.com> wrote in message
news:69**********************************@microsof t.com...
| Hi,
|
| How would I make a class implementing a Singleton Pattern serializable.
| My problem is the Singleton Pattern 'requires' the constructor to be
Private
| and serialization requires the constructor to be Public.
|
| Thanks,
|
| Michael

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.