protobuf-net 将基类反序列化为继承类

发布于 2024-11-18 00:06:32 字数 676 浏览 5 评论 0原文

我有序列化的基类。

[ProtoContract]
public class Web2PdfEntity 
{   


        [ProtoMember(1)]
        public string Title { get; set; }
        [ProtoMember(2)]
        public string CUrl { get; set; }
}

我想将 Web2PdfEntity 类反序列化为从 Web2PdfEntity 继承的 Web2PdfServer 。

public class Web2PdfServer : Web2PdfEntity
{

public void MyServerMethod {}
public void MyServerMethod2{}
}

我尝试使用下面的代码来反序列化类,不幸的是未设置属性。

var web2Pdf = Serializer.Deserialize<Web2PdfServer>("c:\Web2PdfEntity-class-to-serialize-file.bin");
web2Pdf.Title //<- not deserialized
web2Pdf.CURL //<- not deserialized

I have base class which is serialized.

[ProtoContract]
public class Web2PdfEntity 
{   


        [ProtoMember(1)]
        public string Title { get; set; }
        [ProtoMember(2)]
        public string CUrl { get; set; }
}

I would like to deserialize Web2PdfEntity class to Web2PdfServer which is inherited from Web2PdfEntity.

public class Web2PdfServer : Web2PdfEntity
{

public void MyServerMethod {}
public void MyServerMethod2{}
}

I have tried to use code below to deserialize class, unfortunately the properties are not set.

var web2Pdf = Serializer.Deserialize<Web2PdfServer>("c:\Web2PdfEntity-class-to-serialize-file.bin");
web2Pdf.Title //<- not deserialized
web2Pdf.CURL //<- not deserialized

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

欲拥i 2024-11-25 00:06:32

(大量修改)

根据评论,提出的场景是:

  • 有两种类型,碰巧
  • 序列化中C#中的子类,我们只想在它们之间进行平面交换 - 没有继承代码(即因此您可以另存为 Web2PdfEntity 并加载为 Web2PdfServer 或 vv)

这与正常用例略有不同,继承类型需要继承类型期间继承序列化(改变数据)和不相关的类型是可以互换的,只要合同合适。

有几种方法可以解决这个问题;一个小问题是,默认它不会查看继承的属性,以避免重复。您可以重新为它们做广告,但这有点笨拙。就个人而言,我认为我很想告诉它在应用程序启动期间要做什么:

var metaType = RuntimeTypeModel.Default.Add(typeof(Web2PdfServer), false);
metaType.Add(1, "Title").Add(2, "CUrl");

现在您现有的 Serializer 代码将正确处理 Web2PdfServer,包括这两个属性表明的。

(heavily revised)

Based on the comments, the scenario presented is:

  • there are two types, which happen to be subclasses in C#
  • in serialization, we just want to flatly swap between them - no inheritance code (i.e. so you can save as Web2PdfEntity and load as Web2PdfServer, or v.v.)

This is a little different to the normal use case, where inherited types expect inheritance during serialization (which changes the data), and unrelated types are interchangeable as long as the contract fits.

There are a couple of ways of approaching this; one minor issue is that by default it doesn't look at inherited properties, to avoid duplication. You could re-advertise them, but that is a bit ungainly. Personally, I think I'd be tempted to just tell it what to do during app-startup:

var metaType = RuntimeTypeModel.Default.Add(typeof(Web2PdfServer), false);
metaType.Add(1, "Title").Add(2, "CUrl");

Now your existing Serializer code will treat Web2PdfServer correctly, including the two properties as indicated.

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