1

I want to serialize a List on a function like that:

public static void SaveEntriesToDisk(List<IDiaryEntry> entries)
{
  using(var configFile = new IsolatedStorageFileStream(FileName, FileMode.Create, FileAccess.Write))
  {
        var launcherType = entries.GetType();
        //write the concrete type so we know what to deserialize
        string launcherTypeName = launcherType.AssemblyQualifiedName;
        var sw = new StreamWriter(configFile);
        sw.WriteLine(launcherTypeName);
        //write the object itself
        var serializer = new System.Xml.Serialization.XmlSerializer(launcherType);
        serializer.Serialize(sw, entries);    
   }
}

I saw this web: How to Serialize Interfaces in .NET

If I execute this code with one type of IDiaryEntry it is run perfectly but for a List the XmlSerialization throw an exception like that:

...Entities.Contracts.IDiaryEntry cannot be serialized because it is an interface at System.Xml.Serialization.ReflectionHelper.....

Can anybody help me?

Jnavero
  • 353
  • 6
  • 13
  • The link you've provided is self-explanatory, since you cannot serialize an interface, you serialize a derived type of it and when you deserialize you simply cast back to that interface. – aybe Aug 27 '14 at 20:18
  • Yes, but my problem is that I use DI with spring.net. I want to abstracting the class and I am not sure if I have casting each item of List – Jnavero Aug 27 '14 at 20:49
  • Have you seen this http://stackoverflow.com/questions/3704807/c-sharp-xmlserializer-serialize-generic-list-of-interface ? – George Vovos Aug 27 '14 at 21:18
  • No, I had not seen. After that, the two first answers are not valid for me because like I use spring.net I can not know the type of class. Only I can see the last solution like a possibility (BinaryFormatter) – Jnavero Aug 27 '14 at 21:43
  • You simply cannot serialize an interface with the XML Serializer. – John Saunders Aug 28 '14 at 22:22

0 Answers0