I am making a small game to illustrate abstract class, abstract method
and static variables in C#. I try to serialize properties of Tribe Class through MainHouse class that inherited from Tribe Class.
But problem is: I cannot serialize properties of Tribe class because they are declared as static
In this case, How can I serialize properties in Tribe class?
Thanks in advance.
abstract public class Tribe
{
static public int sum_people = 5;
static public int sum_lumber = 200;
abstract public void BearChild();
}
public class MainHouse : Tribe
{
public override void BearChild()
{
if (Tribe.sum_lumber > 50)
{
Tribe.sum_lumber -= 50;
Tribe.sum_people += 1;
}
}
}
public partial class Form1 : Form
{
MainHouse mainhouse = new MainHouse();
public Form1()
{
InitializeComponent();
}
private void Save_Click(object sender, EventArgs e)
{
TextWriter tr = new StreamWriter("Tribe.xml");
XmlSerializer sr = new XmlSerializer(typeof(MainHouse));
sr.Serialize(tr, mainhouse);
tr.Close();
MessageBox.Show("serialized!");
}
}
Loading
AlanPosted Nov 18, 2008, 11:03 AM
The short answer is that you can't really do this.
Serialization is designed to work with specific objects i.e. instance fields, whereas static fields belong to the class as a whole and persist throughout the application's lifetime.
If you could serialize the static fields, then by the time the object was deserialized the static fields could have been changed either directly or via another object of the same class. You could therefore be restoring an earlier value to them!
Just as a matter of interest, if you use binary rather than XML serialization, it is possible to serialize the static fields at their current values by implementing ISerializable and then serializing the static fields manually in the GetObjectData() method and deserializing them in a corresponding constructor.
There's an example of this technique in this thread:
http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/687d1222-404e-4009-974e-9189280faa96/
AlanPosted Nov 19, 2008, 4:34 PM
The XmlSerializer is really designed for use by web services where it's ability to serialize to a pure XML format is valuable.
For anything else, I'd use the binary formatter which is faster, more flexible and produces more compact output.
As for serializing controls, you can't use the binary formatter because the Control class isn't marked with the Serializable attribute and you can't use the XmlSerializer either because of the internal complexity of controls.
You might be able to do something by creating a 'surrogate' class which implements the ISerializationSurrogate interface (see the articles below) but, frankly, I don't think it's worth the hassle:
http://msdn.microsoft.com/en-gb/magazine/cc188950.aspx
http://www.codeproject.com/KB/dotnet/Surrogate_Serialization.aspx?display=Print
kPosted Nov 19, 2008, 7:41 AM
In my game, I need to make Save and Load function, Is there any way to serialize the statuses of each control(buttons, pictureboxes) in my form to file?
kPosted Nov 19, 2008, 12:14 AM
In summary, to serialize static fields, we use BinaryFormatter and SoapFormatter.
Can you tell me some advantages in using XmlSerialize?