使用 protobuf-net 合并 Collection 属性对象

发布于 2024-10-27 12:39:56 字数 2969 浏览 3 评论 0原文

尝试使用 protobuf-net Merge 保存和恢复对象。 我可以让简单的对象工作,但无法让集合属性对象进行合并。 感谢 protobuf-net 专家的任何帮助。

working code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using ProtoBuf;
class Program
{
    static void Main(string[] args)
    {
        ChildTest();
        ParentTest();

        Console.Write("Press any key to continue...");
        Console.ReadKey();
    }

    private static void ChildTest()
    {
        Console.WriteLine("ChildTest:");
        var child = new Child() {Name = "OriginalName"};

        var stream = new MemoryStream();
        Serializer.Serialize(stream, child);
        var tempChild = Serializer.Deserialize<Child>(new MemoryStream(stream.ToArray()));
        tempChild.Name = "MergedName";

        var stream1 = new MemoryStream();
        Serializer.Serialize(stream1, tempChild);
        child = Serializer.Deserialize<Child>(new MemoryStream(stream1.ToArray()));
        Console.WriteLine(child.Name + " - MergedName as expected");
    }

    private static void ParentTest()
    {
        Console.WriteLine("ParentTest:");
        var child = new Child() {Name = "OriginalName"};
        var parent = new Parent {Items = new List<Child>(1)};
        parent.Items.Add(child);

        var stream = new MemoryStream();
        Serializer.Serialize(stream,parent);
        var tempParent = Serializer.Deserialize<Parent>(new MemoryStream(stream.ToArray()));
        tempParent.Items[0].Name = "MergedName";

        var stream1 = new MemoryStream();
        Serializer.Serialize(stream1, tempParent);
        parent = Serializer.Merge(new MemoryStream(stream1.ToArray()), parent);
        Console.WriteLine(parent.Items[0].Name + " - MergedName expected here");

        Parent nullParent = null;
        nullParent = Serializer.Merge(new MemoryStream(stream1.ToArray()), nullParent);
        Console.WriteLine(nullParent.Items[0].Name+ " - MergedName as expected for null");
    }
}

[ProtoContract] //, ProtoInclude(2, typeof(Child[]))
public partial class Parent : ISerializable
{
    public Parent() { }

    [ProtoMember(1)]
    public List<Child> Items { get; set; }

    protected Parent(SerializationInfo info, StreamingContext context)
    {
        Serializer.Merge<Parent>(info, this);
    }
    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        Serializer.Serialize(info, this);
    }
}

[ProtoContract]
public partial class Child : ISerializable
{
    public Child() { }

    [ProtoMember(1)]
    public string Name { get; set; }

    protected Child(SerializationInfo info, StreamingContext context)
    {
        Serializer.Merge<Child>(info, this);
    }
    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        Serializer.Serialize(info, this);
    }
}

显然这不能用 protobuf-net 来完成,- 有什么方法可以做我想做的事情吗?即覆盖重复的属性?也许是另一个图书馆?

Trying to save and restore objects using protobuf-net Merge.
I can get simple objects to work, but I cannot get collection property objects to Merge.
Any help from protobuf-net experts is appreciated.

working code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using ProtoBuf;
class Program
{
    static void Main(string[] args)
    {
        ChildTest();
        ParentTest();

        Console.Write("Press any key to continue...");
        Console.ReadKey();
    }

    private static void ChildTest()
    {
        Console.WriteLine("ChildTest:");
        var child = new Child() {Name = "OriginalName"};

        var stream = new MemoryStream();
        Serializer.Serialize(stream, child);
        var tempChild = Serializer.Deserialize<Child>(new MemoryStream(stream.ToArray()));
        tempChild.Name = "MergedName";

        var stream1 = new MemoryStream();
        Serializer.Serialize(stream1, tempChild);
        child = Serializer.Deserialize<Child>(new MemoryStream(stream1.ToArray()));
        Console.WriteLine(child.Name + " - MergedName as expected");
    }

    private static void ParentTest()
    {
        Console.WriteLine("ParentTest:");
        var child = new Child() {Name = "OriginalName"};
        var parent = new Parent {Items = new List<Child>(1)};
        parent.Items.Add(child);

        var stream = new MemoryStream();
        Serializer.Serialize(stream,parent);
        var tempParent = Serializer.Deserialize<Parent>(new MemoryStream(stream.ToArray()));
        tempParent.Items[0].Name = "MergedName";

        var stream1 = new MemoryStream();
        Serializer.Serialize(stream1, tempParent);
        parent = Serializer.Merge(new MemoryStream(stream1.ToArray()), parent);
        Console.WriteLine(parent.Items[0].Name + " - MergedName expected here");

        Parent nullParent = null;
        nullParent = Serializer.Merge(new MemoryStream(stream1.ToArray()), nullParent);
        Console.WriteLine(nullParent.Items[0].Name+ " - MergedName as expected for null");
    }
}

[ProtoContract] //, ProtoInclude(2, typeof(Child[]))
public partial class Parent : ISerializable
{
    public Parent() { }

    [ProtoMember(1)]
    public List<Child> Items { get; set; }

    protected Parent(SerializationInfo info, StreamingContext context)
    {
        Serializer.Merge<Parent>(info, this);
    }
    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        Serializer.Serialize(info, this);
    }
}

[ProtoContract]
public partial class Child : ISerializable
{
    public Child() { }

    [ProtoMember(1)]
    public string Name { get; set; }

    protected Child(SerializationInfo info, StreamingContext context)
    {
        Serializer.Merge<Child>(info, this);
    }
    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        Serializer.Serialize(info, this);
    }
}

Apparently this cannot be done with protobuf-net, - Is there some way to do what I am trying to do? i.e overwrite repeated properties ? another library perhaps ?

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

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

发布评论

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

评论(1

陌上芳菲 2024-11-03 12:39:56

这是预期的功能;合并单个属性确实会用合并流中的值覆盖属性;然而,对于重复属性(列表、数组等),行为(根据更广泛的“protobuf”实现系列)是附加来自将合并流添加到列表中。

您会发现:

    Console.WriteLine(parent.Items[0].Name); // prints OriginalName
    Console.WriteLine(parent.Items[1].Name) // prints MergedName

具体来说,它按位置进行逐项合并。

This is the expected functionality; merging a singular property does indeed overwrite the properties with the values from the merging stream; however, for repeated properties (lists, arrays, etc) the behavior (as per the wider "protobuf" family of implementations) is to append the additional object(s) from the merging stream to the list.

You will find that:

    Console.WriteLine(parent.Items[0].Name); // prints OriginalName
    Console.WriteLine(parent.Items[1].Name) // prints MergedName

And specifically, it does not do an item-by-item merge by position.

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