限制 protobuf-net 继承“树”

发布于 2024-12-08 09:39:23 字数 583 浏览 1 评论 0原文

继续我的探索,让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相思碎 2024-12-15 09:39:23

通常,该库非常注重发现派生类 - 并将它们与基类区别对待。当前唯一的例外是代理类,特别是实体框架和 NHibernate。对于一个整洁的解决方案,添加某种“忽略子类”开关似乎很实用。但是,虽然这种情况不存在,但非常懒(和黑客)的方法是使用 NHibernate 的现有处理进行欺骗,例如:

namespace NHibernate.Proxy {
    interface INHibernateProxy {}
}
...
public class SomeDerivedType : BaseType, INHibernateProxy {}

然后,这将根据 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:

namespace NHibernate.Proxy {
    interface INHibernateProxy {}
}
...
public class SomeDerivedType : BaseType, INHibernateProxy {}

this will then automatically be serialized as per BaseType. It does have a faint whiff of cheating about it, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文