Serializing an interface object

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have an object which contains an interface member
ie:
[XmlRoot]
public class MyClass
{
[XmlElement]
public IMyInterface implementingClass;
}

where the implementing class is set at runtime to a valid object which is
marked up for XmlSerialization. But when I try to declare a XmlSerializer
class it throws an exception saying that an interface cannot be serialized,
even though the class at runtime can be.

Is there anyway of marking the interface as XmlSerializable or a
method/interface that I can implement that will take care of that
functionality for me?

Thanks in advance for any help!
Matt
 
Figured it out on my own.
I had to wrap the field in a property which was of a XmlSerializable type,
then add a method to my interface which returns that type.

ie:
[XmlElement("myElement")]
public XmlElement myElement
{
get { return myInterface.GetElement(); }
}

Just in case anyone reading this post wants to know how to do it.
 
Back
Top