[Serializable]
class GameObject : PictureBox
{
public bool Solid;
public bool Selected;
}
有没有办法序列化BackColor
,Size
,Location
等......?
答案 0 :(得分:0)
对于临时的小型对象,我更喜欢使用结构,尽管您可以根据需要使用类。您可以使用这些辅助方法序列化和反序列化对象。
public static string Serialize<T>(T objectToSerialize)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
StringWriter textWriter = new StringWriter();
xmlSerializer.Serialize(textWriter, objectToSerialize);
return textWriter.ToString();
}
public static T Deserialize<T>(string stringToDeserialize)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
StringReader textReader = new StringReader(stringToDeserialize);
return (T)xmlSerializer.Deserialize(textReader);
}
如何序列化对象到字符串:
MyStructure myObject = GetPictureBoxObject();
string pbSerializedString = Serialize<MyStructure>(myObject);
如何对字符串到对象进行DeSerialize:
string str = GetStringToDeserialize();
MyStructure myObject = Deserialize<MyStructure>(str);