Not Tested: When you use XmlSerialization you could try to decorate your Property with an [XmlElement] Attribute for all known Implementations.
public interface IMyInterface
{
[XmlElement(Type=typeof(App.Projekt), ElementName="Projekt")]
[XmlElement(Type=typeof(App.Person), ElementName="Person")]
[XmlElement(Type=typeof(App.Task), ElementName="Task")]
IMyInterface parent
{
get;
set;
}
}
Not tested - I dont know if this would also work on interfaces.
EDIT: I tested this issue with this code. It didn't work. I thought, that the XmlElement would do the same as with an Property of type "object".
public interface IMyInterface
{
IMyInterface Parent { get; set; }
string Name { get; set; }
}
public class ClassA : IMyInterface
{
[XmlElement(Type = typeof(ClassA), ElementName = "ClassA")]
[XmlElement(Type = typeof(ClassB), ElementName = "ClassB")]
[XmlElement(Type = typeof(ClassC), ElementName = "ClassC")]
public IMyInterface Parent { get; set; }
public string Name { get; set; }
public string AProperty { get; set; }
}
public class ClassB : IMyInterface
{
[XmlElement(Type = typeof(ClassA), ElementName = "ClassA")]
[XmlElement(Type = typeof(ClassB), ElementName = "ClassB")]
[XmlElement(Type = typeof(ClassC), ElementName = "ClassC")]
public IMyInterface Parent { get; set; }
public string Name { get; set; }
public string BProperty { get; set; }
}
public class ClassC : IMyInterface
{
[XmlElement(Type = typeof(ClassA), ElementName = "ClassA")]
[XmlElement(Type = typeof(ClassB), ElementName = "ClassB")]
[XmlElement(Type = typeof(ClassC), ElementName = "ClassC")]
public IMyInterface Parent { get; set; }
public string Name { get; set; }
public string CProperty { get; set; }
}
Exception was:
"Cannot serialize member
TestXMLSerializer.ClassA.Parent of
type TestXMLSerializer.IMyInterface
because it is an interface."