XmlSerializer serialize generic List of interface

后端 未结 5 1401
别跟我提以往
别跟我提以往 2020-11-28 12:02

I\'m trying to use the XmlSerializer to persist a List(T) where T is an interface. The serializer does not like interfaces. I\'m curious if there is a simple way to serializ

5条回答
  •  醉梦人生
    2020-11-28 12:49

    Do you have to use XmlSerializer? This is a known issue with XmlSerializer.

    You can use BinaryFormatter to save to a stream:

    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream ms = new MemoryStream();
    bf.Serialize(ms, animals);
    

    Other alternative is to use WCF's DataContractSerializer and provide types using KnownType attribute.

提交回复
热议问题