Serializing an interface object

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
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top