c# – ProtoBuf-Net错误消息 – “不支持嵌套或锯齿状列表和数组
发布时间:2020-12-15 08:05:31 所属栏目:百科 来源:网络整理
导读:主要目标是动态填充Dictionary对象并使用Protobuf-net对其进行序列化,并将其通过WCF服务发送到客户端. @marc你能告诉我一个代码示例,当我尝试使用Protobuf-net进行序列化时,如何解决错误“不支持嵌套或锯齿状列表和数组”. [Serializable][ProtoContract]pub
主要目标是动态填充Dictionary对象并使用Protobuf-net对其进行序列化,并将其通过WCF服务发送到客户端.
@marc你能告诉我一个代码示例,当我尝试使用Protobuf-net进行序列化时,如何解决错误“不支持嵌套或锯齿状列表和数组”. [Serializable] [ProtoContract] public class DynamicWrapper { [ProtoMember(1)] public List<Dictionary<string,string>> Items { get; set; } public DynamicWrapper() { Items = new List<Dictionary<string,string>>(); } } public class Service1 : IService1 { public byte[] GetData(string sVisibleColumnList) { DynamicWrapper dw = new DynamicWrapper(); Dictionary<string,string> d = new Dictionary<string,string>(); d.Add("CUSIP","123456"); dw.Items.Add(d); d = new Dictionary<string,string>(); d.Add("ISIN","456789"); dw.Items.Add(d); var ms = new MemoryStream(); var model = ProtoBuf.Meta.RuntimeTypeModel.Default; model.Serialize(ms,dw); Serializer.Serialize <DynamicWrapper>(ms,dw); return ms.GetBuffer(); } } 解决方法
您可以将Dictionary包装到单独的类中.然后使用这些对象的列表.
[ProtoContract] public class DynamicWrapper { [ProtoMember(1)] public List<DictWrapper> Items { get; set; } public DynamicWrapper() { Items = new List<DictWrapper>(); } } [ProtoContract] public class DictWrapper { [ProtoMember(1)] public Dictionary<string,string> Dictionary { get; set; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |