protobuf网序列化System.Object的随着DynamicType抛出异常 [英] protobuf-net Serializing System.Object With DynamicType Throws Exception

查看:372
本文介绍了protobuf网序列化System.Object的随着DynamicType抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我序列化的消息在发送使用protobuf网线。每个消息具有键 - 值对的标头信息的列表。

In my application, I'm serializing messages to send over the wire using protobuf-net. Each message has a list of key-value pairs for header information.

不过,我遇到了一个例外,我已经能够用非常简单的例子重现:

However, I'm running into an exception and I've been able to reproduce it with a very simplified example:

[TestFixture]
public class SerializationTests
{
    [ProtoContract]
    public class MyType
    {
        [ProtoMember(1, DynamicType = true)]
        public object Property { get; set; }
    }

    [Test]
    public void SerializationTest()
    {
        var myType = new MyType {Property = DateTime.UtcNow.ToBinary()};
        Action action = () => myType.Serialize();
        action.ShouldNotThrow();
    }
}

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

此测试失败,目前的异常:System.InvalidOperationException:动态类型是不是合同类型:Int64的。

This test currently fails with the exception: System.InvalidOperationException: "Dynamic type is not a contract-type: Int64".

该物业类型的对象,这样我就可以把各种数据在那里 - 因为这是头信息。我试图避免多个标题列表,其中每一个是强类型。

The property is of type object so I can put various data in there - since this is header information. I'm trying to avoid having multiple header lists where each one is strongly typed.

如果我更改属性为long类型,那么测试工作。如果我删除DynamicType = true,那么我得到一个异常,表示没有串行存在类型的对象。

If I change Property to be of type long, then the test works. If I remove the DynamicType=true, then I get an exception indicating that no serializer exists for type object.

因为当我改变属性的类型的测试工作,这似乎暗示DynamicType和长期的不在一起工作。

Since the test works when I change the type of Property, that seems to imply that DynamicType and long's don't work together.

我目前使用的R640(我相信这是对的NuGet最新的)。

I'm currently using r640 (I believe that's the latest on NuGet).

推荐答案

动态类型不支持原语的当前实现。它仅支持合同类型(已被莫名其妙地定义为其他类 ProtoContract )。

The current implementation of Dynamic type does not support primitives. It only supports contract types (other classes which have are somehow defined as ProtoContract).

维基

DynamicType - 存储与类型的其它类型信息(默认它包括AssemblyQualifiedName,虽然这可以由用户来控制)。这使得序列化弱模型,即在那里对象用于物业成员,但目前这是合同的类型(而不是原语)的限制,以及各类继承不工作(这些限制可能会在以后的时间被删除) 。像与AsReference,使用了一种非常不同的布局格式

DynamicType - stores additional Type information with the type (by default it includes the AssemblyQualifiedName, although this can be controlled by the user). This makes it possible to serialize weak models, i.e. where object is used for property members, however currently this is limited to contract types (not primitives), and does not work for types with inheritance (these limitations may be removed at a later time). Like with AsReference, this uses a very different layout format

这篇关于protobuf网序列化System.Object的随着DynamicType抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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