Serialization of <string, object> dictionary in ProtoBuf-net is not performed
(NOTE: A dictionary where T is some ProtoContract / ProtoMembered class works fine.) This problem occurred only for me with an object of type.
I tried to serialize a dictionary dictionary.
typeof (object) does not work. Should it? Should I do row-based work?
In this case, the object will only ever be a primitive .net.
[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 tried to determine these types ahead of time ... but I think they are processed by protobuf-net by default
//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 suggested and included in my list, but: type processing with built-in serialization (int, etc.) as part of the inheritance has some technical problems that are not very interesting. My recommendation here is to use an abstract base class with a universal concrete implementation and an βincludeβ attribute for the base type to quote each of the expected types at runtime - Foo<int> , Foo<string> , etc. DynamicType will also be here, but without a few minor tricks, I don't think this works immediately for a dictionary. That might do, however.