Make protobuf-net serialize subclasses as base class?

I am using Protobuf-net (with Servicestack). I have a situation where I would like a certain class to be serialized as this exact class, even if the particular instance is inherited. Can Protobuf-net do this? I am not interested in heirs and I never want them to ship to my client who does not have the right material to deserialize.

+3


source to share


1 answer


For now, the answer to that is no. Annoyingly, it has most of the code to support this as it works with proxy types for things like Entity Framework, NHibernate, etc. (Which are implemented as subtypes to be treated as a base type) but I have not yet made it an optional "ignore subtypes" function. In theory, this should be a wiring case. If your inheritance model is just one extra layer (i.e. the type of contract Foo

and you give it SuperFoo

where SuperFoo : Foo

), then it can be tricked into pretending to be NHibernate (it checks interface / namespace names - in fact does not reference NHibernate to reduce dependencies):

namespace NHibernate.Proxy.DynamicProxy
{
    interface IProxy { }
}
...
class SuperFoo : Foo, IProxy {...}

      



However! I have to add:

  • this is, in my opinion, something that I would like to support more directly / officially
  • the above approach will slow things down a bit as it checks this through reflection: to do it right, I would like to make it "direct" (regular type checks, no reflection at runtime)
+1


source







All Articles