protobuf的和List<对象> - 如何序列化/反序列化? [英] protobuf and List<object> - how to serialize / deserialize?

查看:587
本文介绍了protobuf的和List<对象> - 如何序列化/反序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名单,其中,对象> 与不同类型的对象,它像整数,字符串和自定义类型。所有的自定义类型的protobuf调整。 我想现在要做的就是序列化/反序列化这个列表protobuf.net。到现在为止,我怀疑我必须明确地声明每个类型,这是不幸的是不可能的这些混合列表结构。因为二进制格式化器没有问题,做这些事情,我希望我错过了什么,而且你能帮助我。 所以我的问题是如何处理在protobuf.net对象。

I have a List<object> with different types of objects in it like integers, strings, and custom types. All custom types are protobuf-adjusted. What I wanna do now is to serialize / deserialize this list with protobuf.net. Up until now I suspect that I have to declare each and every type explicitly, which is unfortunately not possible with these mixed-list constructs. Because the binary formater has no problems to do these things I hope that I missed something and that you can help me out. So my question is how to deal with objects in protobuf.net.

推荐答案

(披露:我protobuf网的作者)

(disclosure: I'm the author of protobuf-net)

的BinaryFormatter 是一个基于元数据的串行器;也就是说,它发送关于序列中的每个对象的.NET类型信息。 protobuf网是一个基于合同的序列化(的XmlSerializer / 的DataContractSerializer 的等效二进制数,这也将拒绝此)

BinaryFormatter is a metadata-based serializer; i.e. it sends .NET type information about every object serialized. protobuf-net is a contract-based serializer (the binary equivalent of XmlSerializer / DataContractSerializer, which will also reject this).

目前没有任何机制来传送的任意的对象,因为另一端会有的没有办法的知道你要发送什么;但是,如果你有一个的组已知不同的的对象,你要发送的类型,有可能的选择。这里也工作在流水线允许运行时的可扩展架构(而不仅仅是属性,它是固定在编译) - 但这还远远没有完成。

There is no current mechanism for transporting arbitrary objects, since the other end will have no way of knowing what you are sending; however, if you have a known set of different object types you want to send, there may be options. There is also work in the pipeline to allow runtime-extensible schemas (rather than just attributes, which are fixed at build) - but this is far from complete.


这是不理想,但它的工作原理......它应该是比较容易的时候我已经完成了支持运行时模式的工作:

This isn't ideal, but it works... it should be easier when I've completed the work to support runtime schemas:

using System;
using System.Collections.Generic;
using ProtoBuf;
[ProtoContract]
[ProtoInclude(10, typeof(DataItem<int>))]
[ProtoInclude(11, typeof(DataItem<string>))]
[ProtoInclude(12, typeof(DataItem<DateTime>))]
[ProtoInclude(13, typeof(DataItem<Foo>))]
abstract class DataItem {
    public static DataItem<T> Create<T>(T value) {
        return new DataItem<T>(value);
    }
    public object Value {
        get { return ValueImpl; }
        set { ValueImpl = value; }
    }
    protected abstract object ValueImpl {get;set;}
    protected DataItem() { }
}
[ProtoContract]
sealed class DataItem<T> : DataItem {
    public DataItem() { }
    public DataItem(T value) { Value = value; }
    [ProtoMember(1)]
    public new T Value { get; set; }
    protected override object ValueImpl {
        get { return Value; }
        set { Value = (T)value; }
    }
}
[ProtoContract]
public class Foo {
    [ProtoMember(1)]
    public string Bar { get; set; }
    public override string ToString() {
        return "Foo with Bar=" + Bar;
    }
}
static class Program {
    static void Main() {
        var items = new List<DataItem>();
        items.Add(DataItem.Create(12345));
        items.Add(DataItem.Create(DateTime.Today));
        items.Add(DataItem.Create("abcde"));
        items.Add(DataItem.Create(new Foo { Bar = "Marc" }));
        items.Add(DataItem.Create(67890));

        // serialize and deserialize
        var clone = Serializer.DeepClone(items);
        foreach (DataItem item in clone) {
            Console.WriteLine(item.Value);
        }
    }
}

这篇关于protobuf的和List&LT;对象&gt; - 如何序列化/反序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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