Protobuf-net [de] serialization along assembly boundaries

I have a base class in one assembly and a large number of generated classes in another that inherit from the base class. Using protobuf-net (r282) to serialize a base type list fails when trying to resolve subclassType (line 248 SerializerT.cs) because the subclass is not in the base class assembly. Moving classes together is not the preferred option, and it is very important that I can move around the list.

Here is my base class with tags. Included types are marked with ProtoMember(x) as needed.

 [ProtoContract] [ProtoInclude(1,"SomeItemType")] [ProtoInclude(2,"AnotherItemType")] [ProtoInclude(190,"YetAnotherItemType")] public abstract class BaseItem { } 

As a side note, this is part of evaluating the use of protobuf-net to replace BinaryFormatter for moving data between a desktop application and a SOAP web service.

Can I do anything at all? Is there a better way? Did I just miss something obvious? A separate long-term question is that I have to do something a little different in order to prepare for a possible transition to 3.5?

+4
source share
1 answer

Perhaps the easiest way to use ProtoInclude is with typeof , since it automatically handles a lot of nuances:

 [ProtoInclude(1, typeof(SomeItemType))] 

Alternatively, you can simply use the names corresponding to the assembly like this:

 [ProtoInclude(1,"SomeItemType, SomeRandomAssembly")] 

In a rather peculiar case involving several AppDomain s, I found that you can also work with magic with the AppDomain.TypeResolve event, but this should be avoided if possible. I also have a complete overhaul of the metadata layer in the pipeline, which provides much more flexibility at runtime (instead of declaring everything ever at compilation, which causes some pain above).

+1
source

Source: https://habr.com/ru/post/1301489/


All Articles