問題描述
序列化和反序列化未知的繼承類型 (Serializing and deserializing unknown inherited types)
I started looking at using proto-buf .Net for my serialization needs for media browser.
In the new system we have a entity framework that can be extended with plug-ins.
So, for example, we define a Media class in the core library, and then plug-ins can define subclasses such as Song.
It seems that proto-buf .Net needs to know about all sub types of our core defined base class.
So I am expected to decorate my base class with
[ProtoInclude(2, typeof(Song))]
But ... at that point in time I know nothing about the Song class that does not exist yet.
Is it back to the drawing board? Am I trying to do something the library is not designed for?
參考解法
方法 1:
(from the author)
In the current release, it is fixed to types known to the parent. However, this is a known limitation, so there is a plan to abstract the meta-model so that it can be provided separately at runtime - broadly comparable to how you provide such information to XmlSerializer
(it will still need a way to provide tags (the "2" above) per type).
I have started on this work, but I expect it to take a few weeks to get it fully working (the refactor will also tie into some other changes to help compact framework).
So up to you... if you can wait a few weeks, I hope to have something working.
Note that you would still need some way of generating (unique, repeatable) tags for all the different sub-types - configuration, etc.
(by Sam Saffron、Marc Gravell)