Persistant class

  • Thread starter Thread starter Ole
  • Start date Start date
O

Ole

I'm trying to create a class acting to the user as if it is persistant. The
class fields are saved in a XML file and from within the class I call a
private 'SaveToFile' method that contains the line:
serializer.Serialize(writer, this); - that method works very well!
But when trying to load the class (using a 'LoadFromFile' method) I really
run into problems because the 'this' keyword representing the curret instant
is only read only.

Any help or idea is highly appriciated.

Thanks
Ole
 
You should probably use the Singleton pattern. In a static
constructor, you can create and load the instance.

HTH
Andy
 
You could load it to a variable, then copy all the private members over to
the this instance. The are all visible because although you are different
instances (this and the variable), the are the same class.

Or if you want to be a little clever, you could implement the singleton
design pattern.

Ciaran O'Donnell
 
Ole,

You shouldn't pass "this" to your method. Instead, your load method
should return an instance which it will then use for the user settings.

You should expose your user settings through a property somewhere, and
then that class will be responsible for loading and saving the state that it
returns.

Hope this helps.
 
Ole said:
I'm trying to create a class acting to the user as if it is persistant. The
class fields are saved in a XML file and from within the class I call a
private 'SaveToFile' method that contains the line:
serializer.Serialize(writer, this); - that method works very well!
But when trying to load the class (using a 'LoadFromFile' method) I really
run into problems because the 'this' keyword representing the curret instant
is only read only.

Any help or idea is highly appriciated.

Thanks
Ole

I am not whether I understand you exactly, but if you Serialize into the
xml file, the reverse should just Deserialize it.

So, the object itself should be just created by doing this:

YourClass object=new YourClass ();
XmlSerializer xmlSerializer = new XmlSerializer(object.GetType());
XmlNodeReader xmlNodeReader = new XmlNodeReader(xmlNode);
object=(YourClass )xmlSerializer .Deserialize(xmlNodeReader);

If you want to create the object firstly, and then call the
LoadFromFile, you can create a temporary object firstly, then set all
the properties.

HTH
J.W.
 
Jianwei said:
I am not whether I understand you exactly, but if you Serialize into the
xml file, the reverse should just Deserialize it.

So, the object itself should be just created by doing this:

YourClass object=new YourClass ();

new is redundant here, but this is definitely the way to go.
 
Thanks all!
I can't declare the class as static because I need several instances of it.
I did implement it like suggested by some of you creating a temporary object
that reads the file and then copy the fields manually to the 'this'
instance - it works.

However, is there an easy way to copy each field from the worker instance
into the 'this' instance like
foreach (FieldInfo field in temp.GetType().GetFields())
{ this.field = temp.field 0} ???

Thanks
Ole
 
OK found a solution:
foreach (PropertyInfo property in temp.GetType().GetProperties())
{
property.SetValue(this, property.GetValue(temp, null), null);
}

Thanks
Ole
 
Ciaran O''Donnell wrote:
PropertyInfo's is going to be very slow as its reflection which involves a
lot of sting matching when looking through the meta data.
<...>
Define "very slow".
I did the following test:

Test class with 6 straight get/set properties. string, int, datetime,
custom, byte[], all initialized to some random stuff.

Static method which copies the contents of one instance to another.

Running it the first time which requires JIT'ing the relevant reflection
methods (GetType(), GetProperties(), GetValue(), SetValue()
PropertyInfo?) takes 0.0156 seconds.
Running the second time (Already JIT'd) takes 0 seconds.

Main follows:
TestClass a = new TestClass(), b = new TestClass();
DateTime start = DateTime.Now;
TestClass.Copy(a, b);
Console.WriteLine(DateTime.Now.Subtract(start).ToString());

start = DateTime.Now;
TestClass.Copy(b, a);
Console.WriteLine(DateTime.Now.Subtract(start).ToString());


JB
 
Back
Top