how to serialize a static class

  • Thread starter Thread starter Baranyi Peter
  • Start date Start date
B

Baranyi Peter

Hi,

I try to make an application, where I have to store some user settings.
Since these settings are global for the applciation and I don't want to
instatiate it, the best way would be to store them in a static class, or in
a class with only static members. However I don't know how to deserialize
it, when I want to read it back from an XML file.

What is the standard procedure in this case? How can I store and retrive
such static object?

Thanks:

Peter
 
Baranyi Peter,

I think you are missing the concept of serializing. Serialization is a way
of persisiting a graph of objects (class instances). Static classes cannot
be instantiated, thus cannot participate in any object strucutre and cannot
be serialized in the sense of .NET serialization.

What you can do, though, is when it is time to save the static class
properies you can create object of a special (helper) class. Move your
static class' data there and have this object serialized.
The other possible solution, that comes out of the box in VS2005 is to use
the application's settings to store the values in the static class. For
VS2003 you can look at the Microsoft Application Blocks -
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag2/html/config.asp
for implementation of a class for managing application settings.


HTH
Stoitcho Goutsev (100) [C# MVP]
 
Baranyi,

In addition to what Stoitcho mentioned, the other alternative you have
is to expose your settings as a singleton, as opposed to a static class.
This way, you can serialize/deserialize the settings easily.

Hope this helps.
 
Back
Top