列表的 Protobuf.net 对象图序列化 [英] Protobuf.net object graph serialization for lists

查看:56
本文介绍了列表的 Protobuf.net 对象图序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 protobuf.net 的列表不支持 AsReference,因此我尝试了解决此限制的方法.我创建了一个名为 SuperList 的自定义列表,其中包含包裹在 SuperListItem 类型的对象中的项目,如下所示:

I understand that AsReference is not supported for lists with protobuf.net, so I have attempted a work-around for this limitation. I have created a custom list called SuperList that contains items wrapped in objects of type SuperListItem as follows:

[ProtoContract]
public class SuperList<T> where T : class
{
    [ProtoMember(1)]
    private List<SuperListItem<T>> _items = new List<SuperListItem<T>>();

    public SuperList()
    {
    }

    public int IndexOf(T item)
    {
        int indexOf = -1;
        for (int index = 0; index < _items.Count; index++)
        {
            if (_items[index].Item == item)
            {
                indexOf = index;
                break;
            }
        }
        return indexOf;
    }

    public void Insert(int index, T item)
    {
        _items.Insert(index, new SuperListItem<T>(item));
    }

    public void RemoveAt(int index)
    {
        _items.RemoveAt(index);
    }

    public T this[int index]
    {
        get
        {
            return _items[index].Item;
        }
        set
        {
            _items[index] = new SuperListItem<T>(value);
        }
    }

    public void Add(T item)
    {
        _items.Add(new SuperListItem<T>(item));
    }

    public void Clear()
    {
        _items.Clear();
    }

    public bool Contains(T item)
    {
        bool contains = false;
        foreach (var listItem in _items)
        {
            if (listItem.Item == item)
            {
                contains = true;
                break;
            }
        }
        return contains;
    }

    public void CopyTo(T[] array, int arrayIndex)
    {
        for (int index = arrayIndex; index < _items.Count; index++)
            array[index] = _items[index].Item;
    }

    public int Count
    {
        get { return _items.Count; }
    }

    public bool IsReadOnly
    {
        get { return false; }
    }

    public bool Remove(T item)
    {
        SuperListItem<T> itemToRemove = null;
        foreach (var listItem in _items)
        {
            if (listItem.Item == item)
            {
                itemToRemove = listItem;
                break;
            }
        }
        if (itemToRemove != null)
            _items.Remove(itemToRemove);

        return itemToRemove != null;
    }

    public IEnumerator<T> GetEnumerator()
    {
        foreach(var listItem in _items)
            yield return listItem.Item;
    }
}

[ProtoContract]
public class SuperListItem<T>
{
    [ProtoMember(1, AsReference = true)]
    private readonly T _item;
    public T Item { get { return _item; } }

    private SuperListItem() { }

    public SuperListItem(T item)
    {
        _item = item;
    }
}

我有以下序列化测试代码:

I have the following test code for the serialization:

[ProtoContract]
public class Thing
{
    [ProtoMember(1)]
    private readonly string _name;
    public string Name { get { return _name; } }

    private Thing() { }

    public Thing(string name)
    {
        _name = name;
    }
}

public class ProtoTest3
{
    public void Serialize()
    {
        SuperList<Thing> list = GetListOfThings();

        using (var fs = File.Create(@"c:\temp\things.bin"))
        {
            ProtoBuf.Serializer.Serialize(fs, list);

            fs.Close();
        }

        using (var fs = File.OpenRead(@"c:\temp\things.bin"))
        {
            list = ProtoBuf.Serializer.Deserialize<SuperList<Thing>>(fs);

            Debug.Assert(list[0] == list[2]);

            fs.Close();
        }
    }

    private SuperList<Thing> GetListOfThings()
    {
        var thing1 = new Thing("thing1");
        var thing2 = new Thing("thing2");

        var list = new SuperList<Thing>();
        list.Add(thing1);
        list.Add(thing2);
        list.Add(thing1);

        return list;
    }
}

但是,当我运行代码时,出现异常没有为此对象定义无参数构造函数".这仅仅是 ProtoBuf.Net 的限制,还是我做错了什么.有没有办法解决这个问题?

However, when I run the code, it I get the exception "No parameterless constructor defined for this object". Is it simply a limitation in ProtoBuf.Net, or have I done something wrong. Is there a way around this problem?

推荐答案

澄清;列表的限制只是列表本身不被视为引用.项目 ,只要它们位于标记为 AsReference 的成员上 - 例如:

To clarify; the limitation on lists is simply that the list itself is not treated as a reference. The items are, as long as they are on a member marked AsReference - for example:

    [Test]
    public void SerializeTheEasyWay()
    {
        var list = GetListOfThings();

        using (var fs = File.Create(@"things.bin"))
        {
            ProtoBuf.Serializer.Serialize(fs, list);

            fs.Close();
        }

        using (var fs = File.OpenRead(@"things.bin"))
        {
            list = ProtoBuf.Serializer.Deserialize<MyDto>(fs);

            Assert.AreEqual(3, list.Things.Count);
            Assert.AreNotSame(list.Things[0], list.Things[1]);
            Assert.AreSame(list.Things[0], list.Things[2]);

            fs.Close();
        }
    }

    [ProtoContract]
    public class MyDto
    {
        [ProtoMember(1, AsReference = true)]
        public List<Thing> Things { get; set; }
    }

    private MyDto GetListOfThings()
    {
        var thing1 = new Thing("thing1");
        var thing2 = new Thing("thing2");

        var list = new List<Thing>();
        list.Add(thing1);
        list.Add(thing2);
        list.Add(thing1);

        return new MyDto {Things = list};
    }

(有趣的事实 - 这实际上完全在电线上是一样的)

(and fun fact - this is actually exactly the same on the wire)

确实,但是,在这种情况下,它如何创建实例似乎存在错误;它正在使用 .ctor 并且失败了.我会调查并解决这个问题,但以下也有效:

There does, however, seem to be a bug with how it creates the instance in this case; it is using the .ctor and failing. I will investigate and fix this, however the following also work:

1:公开无参数的.ctor:

        public Thing()
        {
        }

2: 或者,禁用 .ctor:

    [ProtoContract(SkipConstructor = true)]
    public class Thing
    { ...

我将调查为什么私有无参数构造函数在这种情况下不满意.

I will investigate why private parameterless constructors aren't happy in this scenario.

这篇关于列表的 Protobuf.net 对象图序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆