protobuf 和 List<object>- 如何序列化/反序列化? [英] protobuf and List&lt;object&gt; - how to serialize / deserialize?

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

问题描述

我有一个 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-net 的作者)

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

BinaryFormatter 是一个基于元数据的序列化程序;即它发送有关每个序列化对象的 .NET 类型信息.protobuf-net 是一个基于合约的序列化器(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<object>- 如何序列化/反序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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