Serializing interfaces

Solution 1:

No. You can't serialize an interface. Ever. It just told you that.

An interface is nothing more than a description of a set of behaviors. It says nothing about the contents of an instance. In particular, although an instance of a class implementing an interface must implement all of its members, it will certainly have properties of its own which need to be serialized.

How would it be deserialized?

What class would be used to deserialize the interface on the other side?

Solution 2:

Here's an untested shady workaround that i tend to use in principle:

private IList<Class2> myArray;
[XmlIgnore]
public IList<Class2> MyArray
{
    get { return myArray; }
    set { myArray = value; }
}

[XmlElement("MyArray")]
public object MyArraySerializable
{
    get { return MyArray; }
    set { MyArray = value as IList<Class2>; }
}

This will serialize whatever list you may be using as generic object with a type attribute which will tell the deserializer the actual type of the object, so when that object gets deserialized it should be cast to a IList<Class2> again. Remember to provide any types that may be assumed by the interface.


I see no reason why any serializer should be unable to serialize such properties. It is not like you actually try to serialize an interface, you try to serialize an object which implements a certain interface (which is not far different from abstract subclassing, some programming languages even solely operate on interfaces).

When the serializer should serialize that object it knows that the object implements that interface, all it really has to do is serialize it and attach the type attribute (like it does if you serialize abstract classes or just super-classes in general).

Now the deserializer looks at the type and can check if that object in fact implements the required interface and then deserialize it to the respective property.

Solution 3:

Or use the DataContractSerializer instead.