序列化派生类时不包含 ProtoBuf.net 基类属性

发布于 2024-11-09 16:43:56 字数 1174 浏览 0 评论 0原文

使用 ProtoBuf.net 的最新 2.0 beta 版本,我尝试序列化派生类(仅示例),但得到空文件。为什么基类属性没有序列化?

[ProtoContract]
[Serializable]
public class Web2PdfClient : Web2PdfEntity
{

}

[ProtoContract]
[Serializable]
public class Web2PdfEntity : EngineEntity
{

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

}


[ProtoContract]
[Serializable]
public class EngineEntity
{

    public bool Result { get; set; }
    public string ErrorMessage { get; set; }
    public bool IsMembershipActive { get; set; }
    public int ConversionTimeout { get; set; }
    public byte[] FileStorage { get; set; }
}

在使用下面的代码序列化类时,我得到空文件。

var Web2PDF = new Web2PdfClient
                          {                                
                              CUrl = "http://www.google.com",
                              FileName = "test.txt"
                          };
        using (var file = File.Create(@"C:\Users\Administrator\Projects\temp\test.bin"))
        {
            Serializer.Serialize(file, Web2PDF);

        }

Using latest 2.0 beta version of ProtoBuf.net I am trying to serialize derived class(just example) and I get empty file. Why base class properties is not serialized?

[ProtoContract]
[Serializable]
public class Web2PdfClient : Web2PdfEntity
{

}

[ProtoContract]
[Serializable]
public class Web2PdfEntity : EngineEntity
{

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

}


[ProtoContract]
[Serializable]
public class EngineEntity
{

    public bool Result { get; set; }
    public string ErrorMessage { get; set; }
    public bool IsMembershipActive { get; set; }
    public int ConversionTimeout { get; set; }
    public byte[] FileStorage { get; set; }
}

While using code below to serialize class I get empty file.

var Web2PDF = new Web2PdfClient
                          {                                
                              CUrl = "http://www.google.com",
                              FileName = "test.txt"
                          };
        using (var file = File.Create(@"C:\Users\Administrator\Projects\temp\test.bin"))
        {
            Serializer.Serialize(file, Web2PDF);

        }

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

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

发布评论

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

评论(1

所有深爱都是秘密 2024-11-16 16:43:56

事实上,我很惊讶没有抛出异常 - 我会调查!为了使其发挥作用,基本类型必须有一种独特的方式来指示每个子类型。这可以通过属性指定,或者(在 v2 中)在运行时指定。例如:

[ProtoContract]
[Serializable]
public class Web2PdfClient : Web2PdfEntity
{

}

[ProtoContract]
[ProtoInclude(7, typeof(Web2PdfClient))]
[Serializable]
public class Web2PdfEntity : EngineEntity
{ ... }

7 没有什么特别之处,只是它不应该与为该类型定义的任何其他成员发生冲突。可以定义多个子类型(使用不同的标签)。另请注意,protobuf-net 不会查看 [Serialized],因此您不需要它,除非您还使用 BinaryFormatter (或类似的)。

同样,EngineEntity 应宣传其预期子类型,并应指示要序列化的成员(以及针对哪个标签)。

Actually, I'm quite surprised that didn't throw an exception - I will investigate! In order for that to work, the base-type must have a unique way to indicate each of the sub-types. This can be specified via attributes, or (in v2) at runtime. For example:

[ProtoContract]
[Serializable]
public class Web2PdfClient : Web2PdfEntity
{

}

[ProtoContract]
[ProtoInclude(7, typeof(Web2PdfClient))]
[Serializable]
public class Web2PdfEntity : EngineEntity
{ ... }

There's nothing special about 7 except that it shouldn't collide with any other members defined for that type. Multiple subtypes can be defined (with different tags). Note also that protobuf-net doesn't look at [Serializable], so you don't need that unless you are also using BinaryFormatter (or similar).

Similarly, EngineEntity should advertise its expected subtypes, and should indicate the members to serialize (and against which tag).

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