使用 protobuf-net r275 进行 DataContract 序列化

发布于 2024-08-12 11:55:32 字数 849 浏览 8 评论 0原文

我刚刚更新到 r275 版本,它似乎不再正确管理 DataContract 类 通过序列化这个非常简单的类:

[DataContract]
public class ProtoData
{
    [DataMember(Order = 1)]
    private long _id;
    [DataMember(Order = 2)]
    private string _firstName;
    [DataMember(Order = 3)]
    private string _lastName;

    public long Id
    {
        get { return _id; }
        set { _id = value; }
    }

    public string FirstName
    {
        get { return _firstName; }
        set { _firstName = value; }
    }

    public string LastName
    {
        get { return _lastName; }
        set { _lastName = value; }
    }

    public ProtoData(long id, string firstName, string lastName)
    {
        _id = id;
        _firstName = firstName;
        _lastName = lastName;
    }

    public ProtoData()
    {
    }

我得到只有数据契约类(以及此类的列表/数组)可以被处理(错误处理ProtoData)

I just updated to r275 version and it doesn't seem to manage correctly DataContract classes any more
By serializing this very simple class:

[DataContract]
public class ProtoData
{
    [DataMember(Order = 1)]
    private long _id;
    [DataMember(Order = 2)]
    private string _firstName;
    [DataMember(Order = 3)]
    private string _lastName;

    public long Id
    {
        get { return _id; }
        set { _id = value; }
    }

    public string FirstName
    {
        get { return _firstName; }
        set { _firstName = value; }
    }

    public string LastName
    {
        get { return _lastName; }
        set { _lastName = value; }
    }

    public ProtoData(long id, string firstName, string lastName)
    {
        _id = id;
        _firstName = firstName;
        _lastName = lastName;
    }

    public ProtoData()
    {
    }

I get Only data-contract classes (and lists/arrays of such) can be processed (error processing ProtoData)

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

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

发布评论

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

评论(2

無處可尋 2024-08-19 11:55:32

真的吗?那是……奇怪;我本来期望单元测试能够解决这样一个重大的变化。您确定您使用的是正确的版本吗?有一个 2.0 版本(包括 [DataContract] 支持,因为这是 WCF 中的 3.0 扩展)和一个单独的 3.0 版本。您需要 3.0 版本 (NET30.zip)。

使用 r275/NET30 成功测试:

static void Main() {
    ProtoData pd = new ProtoData {
        FirstName = "Marc",
        LastName = "Gravell",
        Id = 23354
    }, clone;
    using (MemoryStream ms = new MemoryStream()) {
        Serializer.Serialize(ms, pd);
        Console.WriteLine(ms.Length);
        ms.Position = 0;
        clone = Serializer.Deserialize<ProtoData>(ms);            
    }
    Console.WriteLine(clone.FirstName);
    Console.WriteLine(clone.LastName);
    Console.WriteLine(clone.Id);
}

输出:

19
Marc
Gravell
23354

Really? that is.... odd; I would have expected the unit tests to spt such a breaking change. Are you sure you are using the right version? There is a 2.0 version (which doesn't include [DataContract] support, since this is in WCF, a 3.0 extension) and a separate 3.0 version. You want the 3.0 version (NET30.zip).

Tested successfully with r275/NET30:

static void Main() {
    ProtoData pd = new ProtoData {
        FirstName = "Marc",
        LastName = "Gravell",
        Id = 23354
    }, clone;
    using (MemoryStream ms = new MemoryStream()) {
        Serializer.Serialize(ms, pd);
        Console.WriteLine(ms.Length);
        ms.Position = 0;
        clone = Serializer.Deserialize<ProtoData>(ms);            
    }
    Console.WriteLine(clone.FirstName);
    Console.WriteLine(clone.LastName);
    Console.WriteLine(clone.Id);
}

With output:

19
Marc
Gravell
23354
黑凤梨 2024-08-19 11:55:32

请尝试以下操作:

  • 删除所有私有成员
  • 使用公共属性

    public string LastName;

  • 使用 [DataMember] 标记所有公共属性

Try the following:

  • Remove all private members
  • Use public properties

    public string LastName;

  • Mark all public properties with [DataMember]

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