Protobuf 继承和泛型

发布于 2024-11-27 10:51:37 字数 2817 浏览 0 评论 0 原文

我正在尝试使用 ProtoBuf net 序列化具有以下格式的类的对象树:

[ProtoContract]
class MySpecialCollectionList<T> : List<MySpecialCollection<T>>
{
    [ProtoMember(1)]
    public string Name { get; set; }
}

[ProtoContract]
class MySpecialCollection<T> : List<Special<T>>
{
    [ProtoMember(1)]
    public string Name { get; set; }
}

[ProtoContract]
class Special<T>
{
    [ProtoMember(1)]
    public string Name { get; set; }
    [ProtoMember(2)]
    public string Description { get; set; }

    [ProtoMember(3)]
    private readonly T _source; 
    T Source { get { return _source; } }

    private Special()
    {
    }

    public Special(T source) 
    { 
        _source = source; 
    }
}

interface IBeast
{
    string Name { get; set; }
}
[ProtoContract]
class Ant : IBeast
{
    [ProtoMember(1)]
    public string Name { get; set; }
}
[ProtoContract]
class Cat : IBeast
{
    [ProtoMember(1)]
    public string Name { get; set; }
}
[ProtoContract]
class Dog : IBeast
{
    [ProtoMember(1)]
    public string Name { get; set; }
}

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        MySpecialCollectionList<IBeast> collectionList = GetSpecialCollectionList();
        using (var fs = File.Create(@"c:\temp\protobuftest.bin"))
        {
            Serializer.Serialize(fs, collectionList);

            fs.Close();
        }
    }

    private MySpecialCollectionList<IBeast> GetSpecialCollectionList()
    {
        var ant = new Ant() { Name = "Mr Ant" };
        var cat = new Cat() { Name = "Mr Cat" };
        var dog = new Dog() { Name = "Mr Dog" };

        var Special = new Special<IBeast>(ant);

        var specialCollection1 = new MySpecialCollection<IBeast>() {
            {new Special<IBeast>(ant)},
            {new Special<IBeast>(cat)},
            {new Special<IBeast>(dog)}
        };
        specialCollection1.Name = "Special Collection1";


        var specialCollection2 = new MySpecialCollection<IBeast>() {
            {new Special<IBeast>(ant)},
            {new Special<IBeast>(dog)}
        };
        specialCollection2.Name = "Special Collection2";

        var specialCollectionList = new MySpecialCollectionList<IBeast>() {
            specialCollection1, specialCollection2 };

        specialCollectionList.Name = "Special Collection List";
        return specialCollectionList;
    }
}

请注意我正在序列化的类 (MySpecialCollectionList) 是如何从 List>,而不仅仅是 List

我正在努力找出将“ProtoInclude”属性放在哪里,以使其序列化 MySpecialCollectionList 中的所有项目。任何帮助将不胜感激。

I am attempting to use ProtoBuf net to serialize an object tree with the classes in the following format:

[ProtoContract]
class MySpecialCollectionList<T> : List<MySpecialCollection<T>>
{
    [ProtoMember(1)]
    public string Name { get; set; }
}

[ProtoContract]
class MySpecialCollection<T> : List<Special<T>>
{
    [ProtoMember(1)]
    public string Name { get; set; }
}

[ProtoContract]
class Special<T>
{
    [ProtoMember(1)]
    public string Name { get; set; }
    [ProtoMember(2)]
    public string Description { get; set; }

    [ProtoMember(3)]
    private readonly T _source; 
    T Source { get { return _source; } }

    private Special()
    {
    }

    public Special(T source) 
    { 
        _source = source; 
    }
}

interface IBeast
{
    string Name { get; set; }
}
[ProtoContract]
class Ant : IBeast
{
    [ProtoMember(1)]
    public string Name { get; set; }
}
[ProtoContract]
class Cat : IBeast
{
    [ProtoMember(1)]
    public string Name { get; set; }
}
[ProtoContract]
class Dog : IBeast
{
    [ProtoMember(1)]
    public string Name { get; set; }
}

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        MySpecialCollectionList<IBeast> collectionList = GetSpecialCollectionList();
        using (var fs = File.Create(@"c:\temp\protobuftest.bin"))
        {
            Serializer.Serialize(fs, collectionList);

            fs.Close();
        }
    }

    private MySpecialCollectionList<IBeast> GetSpecialCollectionList()
    {
        var ant = new Ant() { Name = "Mr Ant" };
        var cat = new Cat() { Name = "Mr Cat" };
        var dog = new Dog() { Name = "Mr Dog" };

        var Special = new Special<IBeast>(ant);

        var specialCollection1 = new MySpecialCollection<IBeast>() {
            {new Special<IBeast>(ant)},
            {new Special<IBeast>(cat)},
            {new Special<IBeast>(dog)}
        };
        specialCollection1.Name = "Special Collection1";


        var specialCollection2 = new MySpecialCollection<IBeast>() {
            {new Special<IBeast>(ant)},
            {new Special<IBeast>(dog)}
        };
        specialCollection2.Name = "Special Collection2";

        var specialCollectionList = new MySpecialCollectionList<IBeast>() {
            specialCollection1, specialCollection2 };

        specialCollectionList.Name = "Special Collection List";
        return specialCollectionList;
    }
}

Notice how the class I am serializing (MySpecialCollectionList<T>) is derived from a List<SomeOtherClass<T>>, not just List<T>.

I am struggling to work out where to put "ProtoInclude" attributes to get this to serialize all the items in the MySpecialCollectionList. Any help would be much appreciated.

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

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

发布评论

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

评论(1

画尸师 2024-12-04 10:51:37

继承在这里不是问题,因为即使 A : BFoo 也不是真的。 :Foo。请注意,protobuf-net 不会使用非默认构造函数,尽管可以跳过构造函数,直接绑定到字段(甚至readonly)。虽然您可能有 6 T,但我(从代码中)看不出您打算使用哪种封闭类型,并且如果已知封闭类型,则应该设置它。

如果您有一个 Foo 和许多从 SomeBaseClass 继承的具体类型,那么标记将在 SomeBaseClass 上消失。

但是,如果您有一个具体的场景我可以用来重现您的问题,我会很乐意看一下。


更新后的重新编辑:

示例中列出了几个关键点:

  • 与大多数绑定 API、XmlSerializer 和 IIRC DataContractSerializer 一样,项目或者是一个列表<强>异或具有值的项目;如果一个集合(实现IList的东西)本身有属性,它们将不会被序列化; 封装在这里优于继承,即一个名称一个列表(而不是 >有一个名称并且一个列表)
  • protobuf-net v1不支持基于接口的序列化; v2 确实如此,但与 XmlSerializer 和 DataContractSerializer 一样,您需要明确告诉它需要期待什么;不过,我们可以将 [ProtoMember] 移动到界面本身上,

这是一个很好的 v2 版本:

using System.Collections.Generic;
using ProtoBuf;
[ProtoContract]
class MySpecialCollectionList<T>
{
    [ProtoMember(1)]
    public string Name { get; set; }

    private readonly List<MySpecialCollection<T>> items = new List<MySpecialCollection<T>>();
    [ProtoMember(2)]
    public List<MySpecialCollection<T>> Items { get { return items; } }
}

[ProtoContract]
class MySpecialCollection<T>
{
    [ProtoMember(1)]
    public string Name { get; set; }

    private readonly List<Special<T>> items = new List<Special<T>>();
    [ProtoMember(2)]
    public List<Special<T>> Items { get { return items; } }
}

[ProtoContract]
class Special<T>
{
    [ProtoMember(1)]
    public string Name { get; set; }
    [ProtoMember(2)]
    public string Description { get; set; }

    [ProtoMember(3)]
    private readonly T _source;
    T Source { get { return _source; } }

    private Special()
    {
    }

    public Special(T source)
    {
        _source = source;
    }
}
[ProtoContract]
[ProtoInclude(2, typeof(Ant))]
[ProtoInclude(3, typeof(Cat))]
[ProtoInclude(4, typeof(Dog))]
interface IBeast
{
    [ProtoMember(1)]
    string Name { get; set; }
}
[ProtoContract]
class Ant : IBeast
{
    public string Name { get; set; }
}
[ProtoContract]
class Cat : IBeast
{
    public string Name { get; set; }
}
[ProtoContract]
class Dog : IBeast
{
    public string Name { get; set; }
}

public static class Form1
{

    private static void Main()
    {
        MySpecialCollectionList<IBeast> collectionList = GetSpecialCollectionList();
        var copy = Serializer.DeepClone(collectionList);
    }

    private static MySpecialCollectionList<IBeast> GetSpecialCollectionList()
    {
        var ant = new Ant() { Name = "Mr Ant" };
        var cat = new Cat() { Name = "Mr Cat" };
        var dog = new Dog() { Name = "Mr Dog" };

        var Special = new Special<IBeast>(ant);

        var specialCollection1 = new MySpecialCollection<IBeast>() {Items =
            {new Special<IBeast>(ant),
            new Special<IBeast>(cat),
            new Special<IBeast>(dog)}
        };
        specialCollection1.Name = "Special Collection1";


        var specialCollection2 = new MySpecialCollection<IBeast>()
        {
            Items =
            {new Special<IBeast>(ant),
            new Special<IBeast>(dog)}
        };
        specialCollection2.Name = "Special Collection2";

        var specialCollectionList = new MySpecialCollectionList<IBeast>()
        {
            Items ={
            specialCollection1, specialCollection2 }
        };

        specialCollectionList.Name = "Special Collection List";
        return specialCollectionList;
    }
}

Inheritance is not an issue here since even if A : B it is not true that Foo<A> : Foo<B>. Note that protobuf-net won't use a non-default constructor, although it is possible to skip the constructor, binding to the field directly (even readonly). While you may have 6 T, I can't see (from the code) that it would ever be in doubt which closed type you intend, and if the closed type is known you should be set.

If you have a Foo<SomeBaseClass> and a number of concrete types inherited from SomeBaseClass then the markers would o on SomeBaseClass.

However, if you have a concrete scenario I can use to reproduce your issue, I'll happily take a look.


Updated re edit:

There are a couple of key points drawn out in the example:

  • in common with most binding APIs, XmlSerializer and IIRC DataContractSerializer, an item is either a list xor an item with values; if a collection (something implementing IList) has properties itself, they will not be serialized; encapsulation is preferred over inheritance here, i.e. something that has a Name and has a list (rather than has a Name and is a list)
  • protobuf-net v1 does not support interface-based serialization; v2 does, but as with XmlSerializer and DataContractSerializer you need to explicitly tell it what things it needs to expect; quite nicely, though, we can move the [ProtoMember] onto the interface itself

Here's a fully working version in v2:

using System.Collections.Generic;
using ProtoBuf;
[ProtoContract]
class MySpecialCollectionList<T>
{
    [ProtoMember(1)]
    public string Name { get; set; }

    private readonly List<MySpecialCollection<T>> items = new List<MySpecialCollection<T>>();
    [ProtoMember(2)]
    public List<MySpecialCollection<T>> Items { get { return items; } }
}

[ProtoContract]
class MySpecialCollection<T>
{
    [ProtoMember(1)]
    public string Name { get; set; }

    private readonly List<Special<T>> items = new List<Special<T>>();
    [ProtoMember(2)]
    public List<Special<T>> Items { get { return items; } }
}

[ProtoContract]
class Special<T>
{
    [ProtoMember(1)]
    public string Name { get; set; }
    [ProtoMember(2)]
    public string Description { get; set; }

    [ProtoMember(3)]
    private readonly T _source;
    T Source { get { return _source; } }

    private Special()
    {
    }

    public Special(T source)
    {
        _source = source;
    }
}
[ProtoContract]
[ProtoInclude(2, typeof(Ant))]
[ProtoInclude(3, typeof(Cat))]
[ProtoInclude(4, typeof(Dog))]
interface IBeast
{
    [ProtoMember(1)]
    string Name { get; set; }
}
[ProtoContract]
class Ant : IBeast
{
    public string Name { get; set; }
}
[ProtoContract]
class Cat : IBeast
{
    public string Name { get; set; }
}
[ProtoContract]
class Dog : IBeast
{
    public string Name { get; set; }
}

public static class Form1
{

    private static void Main()
    {
        MySpecialCollectionList<IBeast> collectionList = GetSpecialCollectionList();
        var copy = Serializer.DeepClone(collectionList);
    }

    private static MySpecialCollectionList<IBeast> GetSpecialCollectionList()
    {
        var ant = new Ant() { Name = "Mr Ant" };
        var cat = new Cat() { Name = "Mr Cat" };
        var dog = new Dog() { Name = "Mr Dog" };

        var Special = new Special<IBeast>(ant);

        var specialCollection1 = new MySpecialCollection<IBeast>() {Items =
            {new Special<IBeast>(ant),
            new Special<IBeast>(cat),
            new Special<IBeast>(dog)}
        };
        specialCollection1.Name = "Special Collection1";


        var specialCollection2 = new MySpecialCollection<IBeast>()
        {
            Items =
            {new Special<IBeast>(ant),
            new Special<IBeast>(dog)}
        };
        specialCollection2.Name = "Special Collection2";

        var specialCollectionList = new MySpecialCollectionList<IBeast>()
        {
            Items ={
            specialCollection1, specialCollection2 }
        };

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