DataContract Serialize abstract class

南楼画角 提交于 2019-12-03 07:44:40
Om Deshmane

Get all the types in all loaded assemblies that implement given abstract class or interface(ref:Implementations of interface through Reflection)

 var allTypes =  AppDomain
            .CurrentDomain
            .GetAssemblies()
            .SelectMany(assembly => assembly.GetTypes())
            .Where(type => typeof(A).IsAssignableFrom(type));

Then create serializer passing allTypes as known types parameter, as below

var serializer = new DataContractSerializer(typeof(A), allTypes);

that's it - you will be able to serialize and deserialize any type that derives from A (A could be class or interface, if interface, serializer writes elements as deriving from xs:anyType.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!