The only way you can do this is to re-implement the interface as a pure
abstract class (or re-present the interface member in a new serializable
class) and XmlInclude the types in the serializable class. It goes something
like this..
given the following class definitions
public class SerializeMe
{
public SerializeMe(parameters) // ctor
{ ... }
public ISomething InterfaceProperty = new Something();
}
public interface ISomething
{
string Member { get; set; }
}
public class Something : ISomething {
public string Member { get { ... } set{ ... } }
}
you can't serialize this with the XmlSerializer. First, you have to add a
default no-args constructor for SerializeMe. and add an XmlInclude attribute
to the class. Like so
[XmlInclude(typeof(Something))]
public class SerializeMe {
....
}
this tells the serializer to allow for instance of Something to appear in
the XML, despite it not being explicitly specified in the class definition.
This is only part of the trick, since you still can't serialize the
interface. However, if you inject a pure abstract class in between the
ISomething interface and the Something class like this:
public abstract class AbstractSomething : ISomething
{
public abstract string Member { get; set; }
// ... implement all interface properties as abstract properties..
}
public class Something : AbstractSomething
{
public override string Member { get { ... } set{ ... } }
}
[XmlInclude(typeof(Something))]
public class SerializeMe
{
public SerializeMe() {}
public SerializeMe(parameters) // ctor
{ ... }
public AbstractSomething InterfaceProperty = new Something();
}
and now the class will serialize. If you want to have different classes
implementing the interface/abstract base, then you will need to include them
with XmlIncludes at the top of the serializable class.
However, what you *can* do is dynamically generate the XmlIncludes when you
construct the XmlSerializer, but this is a bit trickier, and is left for
another day unless you *really* want to know.
Leon