protobuf-net:无法序列化类型数据,如何使用协议缓冲区定义类型数据? [英] protobuf-net: Cannot serialize type data, How can I define type data with protocol buffers?

查看:32
本文介绍了protobuf-net:无法序列化类型数据,如何使用协议缓冲区定义类型数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的内存网格,可以使用协议缓冲区进行序列化.

I am trying to create a simple in-memory grid that can be serialized using protocol buffers.

这个想法是用户可以创建/定义任何类型的列(原语或用户定义的,只要它们是协议缓冲区标记的).

The idea is the user can create/define columns of any type (primitives or user defined as long as they're protocol buffers tagged).

我的问题是您无法使用协议缓冲区序列化类型数据,那么我该如何实现?

My problem is you can't serialize Type data with protocol buffers so how can i achieve this?

下面的代码显示了我希望如何对网格的列进行编码.

Code is below showing how i'd hoped to code the Columns of the grid.

非常感谢.

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        Column colOrderId = new Column("OrderId", typeof(uint));
        Column colOrderDesc = new Column("OrderDesc", typeof(string));
        Column colPrice = new Column("Price", typeof(Price));
        Column colOrderDateTime = new Column("OrderDateTime", typeof(DateTime));

        var s = colOrderId.ToBArray();
    }
}

[ProtoContract, Serializable]
public sealed class Column
{
    public Column(string name, Type type)
    {
        Name = name;
        Type = type;
    }

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

    [ProtoMember(2)]
    public Type Type { get; private set; }
}

[ProtoContract, Serializable]
public sealed class Price
{
    public Price(double value, string currency)
    {
        Value = value;
        Currency = currency;
    }

    [ProtoMember(1)]
    public double Value { get; private set; }

    [ProtoMember(2)]
    public string Currency { get; private set; }
}

public static class ProtoBufEx
{
    public static byte[] ToBArray<T>(this T o)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            ProtoBuf.Serializer.Serialize(ms, o);
            return ms.ToArray();
        }
    }
}

推荐答案

这种方法有几个问题;是的,在标准协议缓冲区中,任何类型的 Type 元数据都有点调皮,因为它不是真正可互操作的,但我在 v2 中在选择加入的基础上有点松散 - 允许您发送 object 等,只要它在内部仍然知道您的意思.

There's a couple of issues in this approach; yes, in standard protocol buffers any kind of Type metadata is a bit naughty, as it isn't really interoperable, but I play a little loose with that in v2 on an opt-in basis - allowing you to send object etc as long as internally it still knows what you mean.

然而,这对于逐个单元的工作会变得昂贵,即使内置优化(例如,它只发送一次相同类型的元).

This, however, will get expensive for cell-by-cell work, even with the optimisations built in (it only sends the same type meta once, for example).

不过,IMO 更好的选择是将自己限制在所有提前已知的已知类型列表中.如果你能做到,还有一些有趣的继承技巧,它们非常有效并且非常适合这种情况.但基本上你会有类似的东西:

IMO, though, a far better option is to restrict yourself to a list of known types that are all known ahead of time. If you can do that there are some interesting tricks with inheritance that are pretty efficient and reasonably suited for this scenario. But essentially you'd have something like:

[ProtoContract]
[ProtoInclude(4, typeof(Column<int>))] // etc
abstract class Column {
    [ProtoMember(1)]
    public string Name {get;private set;}

    public abstract object Value {get;private set;}
}
[ProtoContract]
class Column<T> : Column {
    [ProtoMember(1)]
    public T TypedValue { get;private set;}
    override Value {...shim to TypedValue...}
}
// etc

(伪代码,不完整)

我很乐意为您介绍其中的很多内容,但您可能还想查看 这个博客条目 看看是否使用 DataTable(因为我不喜欢它用于数据访问)在这里可以节省一些精力.

I'd happily walk you through a lot of that, but you might also want to look at this blog entry to see if using DataTable (much as I dislike it for data-access) might save some effort here.

这篇关于protobuf-net:无法序列化类型数据,如何使用协议缓冲区定义类型数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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