I am trying to implement a way to save a set of objects to file, and then read it back to objects again. I want to serialize the objects to XML (or JSON). The objects consists of one master object which holds an array of all the other objects. The array is of the type Interface, to allow several different types of child objects with some common functionality. Obviously, there will be a problem during deserialization because the type of the interface object is not known.
Example:
[Serializable]
public class MasterClass
{
public ImyInterface[] subObjects;
}
public interface ImyInterface
{
}
How can I serialize/deserialize these objects?
My suggestions: Add information about the object type in the serialized data. Use a different solution than interface.
var jsonString = JsonConvert.SerializeObject(objectsToSerialize, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto })Or you can use SubType