[英]Serializing List<Dictionary<string,object>> with Protobuf-Net
[英]serializing a Dictionary<string,object> in ProtoBuf-net fails
(注意:字典,其中T是一些ProtoContract / ProtoMembered類工作正常。)這個問題只發生在我的類型對象上。
我試圖序列化詞典工作字典。
typeof(object)不起作用。 應該是? 我應該實現基於字符串的解決方案嗎?
在這種情況下,對象將只是一個.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);
}
我試圖提前定義這些類型......但我認為默認情況下它們是由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);
直接執行此操作的願望已經提出,並且在我的列表中可以查看,但是:使用內置序列化(int等)作為繼承的一部分處理類型具有一些不太有趣的技術問題。 我的建議是使用具有通用具體實現的抽象基類,並在基類型上使用“include”屬性來引用運行時的每個預期類型Foo<int>
, Foo<string>
等.DynamicType也會在這里考慮一下,但沒有一些小的調整,我認為這不會立即為字典工作。 但它可以做到。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.