Objects of the following class can be serialized to XML as intended, as long as the actual type of the generic field Object is contained in the list of XmlElement attributes:
public class SerializedObject<T> : Serializable where T : Serializable
{
[System.Xml.Serialization.XmlElement(Type = typeof(Weapon))]
[System.Xml.Serialization.XmlElement(Type = typeof(Armor))]
[System.Xml.Serialization.XmlElement(Type = typeof(QuestItem))]
public T Object;
public string ObjectId;
public int ID;
public SerializedObject() { }
public SerializedObject(T _object)
{
Object = _object;
ID = Object.Id;
ObjectId = Object.ObjectId;
}
}
The question is:
How can I serialize an object of this class, including the generic field Object without specifiying all possible types for T in XmlElement attributes?
I ran into this too. What I did is create a wrapper class:
Here is the class used to wrap the object
Now, you can just call it as: