views:

226

answers:

2

I have an interface, with a definintion for a property that is the same type as the interface.

public interface IMyInterface
{
    IMyInterface parent
    {
        get;
        set;
    }
}

Now if I declare a class and inherit from the interface, I need to create the property called parent. I want my class to be serializable to use in a web service, but Interfaces are not serializable when used that way, so what should I do about my property of type IMyInterface? I do want that property to serialize.

A: 

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."

Arthur
Does not appear to work. I also tried using the XmlInclude attribute with no luck either
Jeremy
A: 

Funny thing is: when you replace the interface declaration with an abstract class it works. .. even with lists.

(Well, actually it is not funny...)

public class Root
{
    [XmlElementAttribute("ClassA", typeof(ClassA))]
    [XmlElementAttribute("ClassB", typeof(ClassB))]
    [XmlElementAttribute("ClassC", typeof(ClassC))]
    public List<IMyInterface> Items { get; set; }
}


public abstract class IMyInterface
{
    IMyInterface Parent { get; set; }
    string Name { get; set; }
}
schup