简体   繁体   中英

Serialization of an abstract class without [XMLInclude] or Type[] Constructor

I am not sure whether this is possible or not but basically i have an abstract class in NameSpace1[assembly1] and the derived class in Namespace2[assembly2]. Namespace1[assembly1] is referenced by Namespace2[assembly2].

Now i want to serialize the abstract class after creating an object of the derived class from assembly3[only references assembly1].

I googled and found out that i need to specify [XMLInclude()] attribute or provide the type as ExtraTypes in the XMLSerializer Constructor. The current structure of my project won't allow me to achieve that , Can serialization be achieved without the either of the two methods?

Thanks

In general it is possible.

You can use IXmlSerializable implementation for this usecase. At serialization time you can write down the type of the objects as string into the xml and the objects, properties etc...

For Deserialization you can read the type back from the xml and use Activator.CreateInstance(...) to create the types back again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM