I want to serialize a large tree structure of different objects to a xml
file. To get a loose coupling design some classes have interface members. At
runtime I get an InvalidOperationException: Cannot serialize member XYZ
because it is an interface.
To boil the thing down to a test example:
public interface IProblematicMember
{
}
public class ProblematicMember: IProblematicMember
{
}
public class DoesNotSerialize
{
private IProblematicMember problematicMember;
public IProblematicMember ProblematicMember
{
get { return problematicMember; }
set { problematicMember = value; }
}
}
class Program
{
static void Main(string[] args)
{
DoesNotSerialize dns = new DoesNotSerialize();
XmlSerializer mySerializer = new
XmlSerializer(typeof(DoesNotSerialize));
StreamWriter myWriter = new StreamWriter("myFileName.xml");
mySerializer.Serialize(myWriter, dns);
myWriter.Close();
}
}
Is there any possibility to solve this, e.g. by implementing the
serialization by hand?
Thanks for your help,
Fabian
Frans Bouma [C# MVP] - 08 Dec 2007 10:25 GMT
> I want to serialize a large tree structure of different objects to a
> xml file. To get a loose coupling design some classes have interface
[quoted text clipped - 38 lines]
> Is there any possibility to solve this, e.g. by implementing the
> serialization by hand?
You have to implement IXmlSerializable and indeed do the serialization
by hand.
FB

Signature
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------