限制 protobuf-net 继承“树”
继续我的探索,让 protobuf-net 屈服于我自己的意愿。
我已经看到了一些关于如何动态添加子类的问题 让序列化器能够对子类进行编码..,例如 this 或 这个
我的情况有点不同,我有一个基类,可能会被子类化后期限制代码,我想序列化为 BASE 类,并完全忽略子类的字段/属性。
我需要这个的原因是,稍后,当我反序列化数据时,子类的代码将不可用,因此构造子类甚至是不可能的。
有没有办法限制/禁止子类序列化?
就我而言,我有一个列表,其中列表中的某些项目是 DerivedClass。
我想找到一种方法使 protobuf-net 将所有内容序列化为 BaseClass 并反序列化为 BaseClass ......
我尝试查看代码,但没有找到太有用的东西。
Going on with my quest to bend protobuf-net to my own will..
I've seen a few questions around SO on how to add sub-classes dynamically
for the serializer to be able to encode the sub-class.., like this or this
My situation is bit different, I have a base class that might get sub-classed in late-bounded code, and I want to serialize is as the BASE class, and completely ignore the sub-class's fields/properties.
The reason I need this, is that later on, when I deserialize the data, the sub-class's code will not be even available, so constructing the sub-class will not be even possible.
Is there a way to limit/prohibit sub-class serializtion?
In my case I have a List where some items in the list are DerivedClass.
I would like to find a way to make protobuf-net serialize everything as BaseClass and to deserialize to BaseClass as well...
I've tried peering into code, but haven't found something too useful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常,该库非常注重发现派生类 - 并将它们与基类区别对待。当前唯一的例外是代理类,特别是实体框架和 NHibernate。对于一个整洁的解决方案,添加某种“忽略子类”开关似乎很实用。但是,虽然这种情况不存在,但非常懒(和黑客)的方法是使用 NHibernate 的现有处理进行欺骗,例如:
然后,这将根据
BaseType< 自动序列化/代码>。不过,它确实有一丝欺骗的味道。
Normally, the library is very particular about spotting derived classes - and treating them differently from the base class. The only current exception to that is proxy classes, in particular Entity Framework and NHibernate. For a tidy solution, it would seem practical to add some kind of "ignore subclasses" switch. But while that doesn't exist, a very lazy (and hacky) approach would be to cheat using the existing handling for NHibernate, for example:
this will then automatically be serialized as per
BaseType
. It does have a faint whiff of cheating about it, though.