【发布时间】:2015-05-31 12:49:57
【问题描述】:
我收集到,在使用集合时,protobuf-net 需要 GetEnumerator 进行序列化,并使用带有 Add 的类型进行反序列化。
对于没有Add 的类型,您可以设置在反序列化之前执行的继承类型。例如,这适用于 IEnumerable 和 List:
[ProtoContract(ImplicitFields = ImplicitFields.AllPublic)]
public class Sheep
{
public IEnumerable<string> Children { get; set; }
public Sheep()
{
Children = new List<string>();
}
}
var dolly = RuntimeTypeModel.Default.DeepClone(new Sheep
{
Children = new[]
{
"Bonnie",
"Sally",
"Rosie",
"Lucy",
"Darcy",
"Cotton"
}
});
但是,当我对IReadOnlyCollection(或IReadOnlyList)做同样的事情时:
[ProtoContract(ImplicitFields = ImplicitFields.AllPublic)]
public class Sheep
{
public IReadOnlyCollection<string> Children { get; set; }
public Sheep()
{
Children = new List<string>();
}
}
我得到一个例外:
无法为 System.Collections.Generic.IReadOnlyCollection`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] 解析合适的 Add 方法]
那么,有没有办法让 protobuf-net 处理 IReadOnlyCollection/IReadOnlyList 的成员?
编辑:我已经在项目的存储库中为此功能打开了一个问题:Support IReadOnlyCollection/IReadOnlyList members
【问题讨论】:
-
您使用的是哪个 protobuf 库?马克的那个?
-
@YuvalItzchakov 是的,protobuf-net。 Skeet 是 protobuf-csharp-port。
标签: c# .net protobuf-net