【发布时间】:2015-11-06 12:58:36
【问题描述】:
我有一个要序列化的字典列表,但出现以下错误:
不支持 System.NotSupportedExceptionNested 或锯齿状列表和数组
甚至可以序列化吗?我尝试将其推送到另一个类并有一个List<Dto>,其中Dto 类是Dictionary 的IEnumerable,字典中的对象是DynamicType,但它仍然没有不工作。
我是完全错误地处理这个问题还是我错过了一些微妙的东西?
示例代码:
var zone = DateTimeZoneProviders.Tzdb["Europe/London"];
_testObj = new Person
{
Name = "Homer",
Age = 38,
Timestamp = new ZonedDateTime(Instant.FromUtc(2013, 6, 12, 17, 53, 23), zone),
Target = new DataCollection(new Dictionary<string, Type>()
{
{"Date", typeof(DateTime)}
}, new List<Dictionary<string, object>>
{
new Dictionary<string, object>
{
{"Date", new DateTime(2015, 1, 1)}
}
}
, new List<string> { "Date" })
还有DataCollection:
[Serializable, ProtoContract(IgnoreListHandling = true), DataContract, JsonObject]
public class DataCollection : IEnumerable<IDictionary<string, object>>
{
public DataCollection(Dictionary<string, Type> fields, List<Dictionary<string, object>> data, List<string> keyFields)
{
Fields = fields;
KeyFields = keyFields;
Data = data;
}
public DataCollection()
{
Fields = new Dictionary<string, Type>();
Data = new List<Dictionary<string, object>>();
KeyFields = new List<string>();
}
[ProtoMember(1), DataMember]
public Dictionary<string, Type> Fields { get; set; }
[ProtoMember(2), DataMember]
public List<string> KeyFields { get; set; }
[ProtoMember(3), DataMember]
public List<Dictionary<string, object>> Data { get; set; }
public void Add(Dictionary<string, object> value)
{
Data.Add(value);
}
public IEnumerator<IDictionary<string, object>> GetEnumerator()
{
return Data.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
如果我不包含 ProtoMember(3),我可以很好地序列化。
【问题讨论】:
-
我应该提到我已经成功地使用了一个代理来序列化 NodaTime 属性,没有任何问题。
标签: c# serialization protobuf-net