序列化字典<字符串对象>在protobuf网失败 [英] serializing a Dictionary<string,object> in ProtoBuf-net fails

查看:252
本文介绍了序列化字典<字符串对象>在protobuf网失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(注意:字典其中T是一些ProtoContract / ProtoMembered类正常工作)这个问题只发生过,我有类型的对象。



我试图词典序列化工作的字典。



的typeof(对象)不工作。应该是?我应该围绕实现一个基于字符串的工作?



在这种情况下,对象将永远只能是一个.NET原始的。

  [测试] 
公共无效De_SerializeObjectDictionary2()
{
变种D =新词典<字符串对象>();

d.Add(ABC,12);

变种MS =新的MemoryStream();

VAR模型= ProtoBuf.Meta.RuntimeTypeModel.Default;
//model.AutoAddMissingTypes = TRUE;
//model.AutoCompile = TRUE;
//model.InferTagFromNameDefault = TRUE;
//model.Add(typeof(对象),FALSE);
//model.Add(typeof(Int32),TRUE);
//模型[typeof运算(对象)AddSubType(50的typeof(Int32)已)。

model.Serialize(MS,D);
Serializer.Serialize<&字典LT;字符串对象>>(MS,D);
//< ---没有类型定义的串行:System.Object的

//或
//model.Add(typeof(对象),FALSE);
//Serializer.Serialize<Dictionary<string,对象>>(MS,D);
//< - 意外的子类型:System.Int32
ms.Position = 0;

VAR D2 = Serializer.Deserialize<&字典LT;字符串对象>>(毫秒);
}






我试图定义这些类型提前...但我认为他们在默认情况下,通过protobuf网

  //model.Add(处理typeof运算(对象),FALSE); 
//模型[typeof运算(对象)AddSubType(50的typeof(Int32)已)。
/ *
//model.Add(typeof(int),FALSE);
//model.Add(typeof(string),FALSE);
//model.Add(typeof(short),FALSE);
//model.Add(typeof(DateTime),FALSE);
//model.Add(typeof(long),FALSE);
//model.Add(typeof(bool),FALSE);
//model.Add(typeof运算(INT []),FALSE);
//model.Add(typeof运算(字符串[]),FALSE);
//model.Add(typeof运算(短[]),FALSE);
//model.Add(typeof运算(DateTime的[]),FALSE);
//model.Add(typeof运算(长[]),FALSE);
//model.Add(typeof运算(布尔[]),FALSE);

//model.Add(typeof(int),假的)?;
//model.Add(typeof(short),假的)?;
//model.Add(typeof(DateTime),假的)?;
//model.Add(typeof(long),假的)?;
//model.Add(typeof(bool),假的)?;
//model.Add(typeof运算(INT []),假的?);
//model.Add(typeof运算(短[]),假的?);
//model.Add(typeof运算(DateTime的[]),假的?);
//model.Add(typeof运算(长[]),假的?);
//model.Add(typeof运算(布尔[]),假的?);

//model.Add(typeof运算(字节[]),FALSE);
//model.Add(typeof(byte),FALSE);


解决方案

已经被提出来直接做到这一点的愿望,是我的名单上看看,但:有内置的序列化(INT等)的治疗类型继承的部分有是不是很有趣一些技术问题。在这里,我的建议是使用与通用的具体实现的抽象基类,以及包含在基本型的属性在运行时引用的每个预期的类型 - 美孚< INT> 美孚<串> 等DynamicType也将是一个考虑这里,但没有一些小的调整,我不认为这个工程立即字典。它可以做,虽然。


(NOTE: Dictionary where T is some ProtoContract / ProtoMembered class works fine. ) This issue only happened for me with type object.

I was trying to serialize a dictionary of Dictionary working.

typeof(object) doesn't work. Should it? Should I implement a string based work around?

In this scenario, object will only ever be a .net primitive.

    [Test]
    public void De_SerializeObjectDictionary2()
    {
        var d = new Dictionary<string, object>();

        d.Add("abc", 12);

        var ms = new MemoryStream();

        var model = ProtoBuf.Meta.RuntimeTypeModel.Default;
        //model.AutoAddMissingTypes = true;
        //model.AutoCompile = true;
        //model.InferTagFromNameDefault = true;
        //model.Add(typeof (object), false);
        //model.Add(typeof(Int32), true);
        //model[typeof (object)].AddSubType(50, typeof (Int32));

        model.Serialize(ms, d);
        Serializer.Serialize<Dictionary<string,object>>(ms, d);
        // <--- No serializer defined for type: System.Object

        // or
        //model.Add(typeof (object), false);
        //Serializer.Serialize<Dictionary<string, object>>(ms, d);
        //<-- Unexpected sub-type: System.Int32
        ms.Position = 0;

        var d2 = Serializer.Deserialize<Dictionary<string, object>>(ms);
    }


I attempted to define these types ahead of time... but I think they're handled by default by protobuf-net

        //model.Add(typeof (object), false);
        //model[typeof (object)].AddSubType(50, typeof (Int32));
        /*
        //model.Add(typeof(int), false);
        //model.Add(typeof(string), false);
        //model.Add(typeof(short), false);
        //model.Add(typeof(DateTime), false);
        //model.Add(typeof(long), false);
        //model.Add(typeof(bool), false);
        //model.Add(typeof(int[]), false);
        //model.Add(typeof(string[]), false);
        //model.Add(typeof(short[]), false);
        //model.Add(typeof(DateTime[]), false);
        //model.Add(typeof(long[]), false);
        //model.Add(typeof(bool[]), false);

        //model.Add(typeof(int?), false);
        //model.Add(typeof(short?), false);
        //model.Add(typeof(DateTime?), false);
        //model.Add(typeof(long?), false);
        //model.Add(typeof(bool?), false);
        //model.Add(typeof(int?[]), false);
        //model.Add(typeof(short?[]), false);
        //model.Add(typeof(DateTime?[]), false);
        //model.Add(typeof(long?[]), false);
        //model.Add(typeof(bool?[]), false);

        //model.Add(typeof(byte[]), false);
        //model.Add(typeof(byte), false);

解决方案

The desire to do this directly has already been proposed, and is on my list to look at, but: treating types with inbuilt serialisation (int etc) as part of inheritance has some technical issues that are not very interesting. My recommendation here is to use an abstract base class with generic concrete implementation, and an "include" attribute on the base-type to cite each of the expected types at runtime - Foo<int>, Foo<string> etc. DynamicType would also be a consideration here, but without a few minor tweaks I don't think this works immediately for dictionary. It could do, though.

这篇关于序列化字典&LT;字符串对象&gt;在protobuf网失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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