Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
788 views
in Technique[技术] by (71.8m points)

c# - serializing a Dictionary<string,object> in ProtoBuf-net fails

(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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...