c# - IL Emit base class name is the same as inherited class; protobuffer does not accept cyclic inheritance -
i have emitted code returns object of type passed in, propertychange notification wrapped virtual properties, modification tracking client. new type shared between client , server (serialized using protobuf.net). i'm limited not using third-party libraries, other use of protobuf.net.
the issue i'm having when try serialize list of new objects (for example, typea) using protobuffer, run "unexpected sub-type: typea", , when try add subtype model protobuffer using runtimetypemodel, run "cyclic inheritance not allowed", protobuffer not accept @ time, afaik.
i'm new reflection.emit - there way type new class emitted different type i'm emitting, @ least in name? may able overcome cyclic inheritance restriction in case. i'd avoid creating/copying new objects.
for instance, emit new object as:
newtypea -base typea
instead of:
typea -base typea -sub-type typea
il emitter:
usage: type atype = createproxy(typea); activator.createinstance(atype); public static type createproxy(type type) { var assmname = new assemblyname("dynamicproxyassembly"); ab = appdomain.currentdomain.definedynamicassembly(assmname, assemblybuilderaccess.run); mb = _ab.definedynamicmodule(assmname.name); typebuilder typebuilder = mb.definetype(type.namespace + "." + type.name + "__proxy", typeattributes.public, type); typebuilder.addinterfaceimplementation(typeof(inotifypropertychanged)); fieldbuilder eventfield = createpropertychangedevent(typebuilder); methodbuilder raisepropertychanged = createraisepropertychanged(typebuilder, eventfield); methodinfo ismodifiedsetmethod = type.getproperty("modified").setmethod; foreach property in type virtual, wrap method propertychangednotification... type ret = typebuilder.createtype(); // returns typea__proxy derived (base=typea__proxy). }
it turns out runtimetypemodel used when serializing through protobuffer. there nothing wrong sub-classing emitted type derived type.
i needed add type of instance instead of typea:
runtimetypemodel.default.add(typeof(typea), true).addsubtype(555, typeainstance.gettype());
...before had:
runtimetypemodel.default.add(typeof(typea), true).addsubtype(555, typea);
which, of course, returned cyclic error.
i hope helps in future.
Comments
Post a Comment