Featured post
c# - DataContract Serialize abstract class -
i have interface iserviceinfo , abstract class serviceinfo. there several classes inherited serviceinfo, coreserviceinfo, moduleserviceinfo etc. there service contract named rootservice returns iserviceinfo.
public iserviceinfo getserviceinfo() { return (iserviceinfo)new coreserviceinfo(); }
i having problem serializing. can use serviceknowntype identify base class, , knowntype identify child class.
problem not know serviceinfo child, since application can have plugins different child inherited serviceinfo, cannot tell serializer have child in serialized xml.
i can ignore abstract class, contains common implementation need keep it. work around can have class "sampleserviceinfo" , convert info classes sampleserviceinfo , return service method, , define knowntype serviceinfo class.
[knowntype(typeof(sampleserviceinfo))] public class serviceinfo : iserviceinfo
but not sound pretty way it. please suggest. need write custom serializer? there way serialize base , ignoring child when both has same members?
get types in loaded assemblies 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 known types parameter, below
var serializer = new datacontractserializer(typeof(a), alltypes);
that's - able serialize , deserialize type derives (a class or interface, if interface, serializer writes elements deriving xs:anytype.
- Get link
- X
- Other Apps
Comments
Post a Comment