Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

Serializing a Singleton

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Joe Tavares - 11 Jan 2008 19:10 GMT
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;
       }

   }
Nicholas Paldino [.NET/C# MVP] - 11 Jan 2008 19:57 GMT
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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

>I  have read several articles on serializing a singleton and have attempted
> to implement it in my code. Unfortunately, the object is not being
[quoted text clipped - 36 lines]
>
>    }
Joe Tavares - 11 Jan 2008 22:59 GMT
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/blogs/simoneb/archive/2007/05/04/When-is-a-s
ingleton-not-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.serializat
ioninfo.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-

> Joe,
>
[quoted text clipped - 57 lines]
> >
> >    }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.