Reputation: 116636
I gather that when working with collections protobuf-net needs GetEnumerator
for serialization and a type with Add
for the deserialization.
For types that don't have an Add
you can set an inheriting type that does before deserialization. This works for example with IEnumerable
and a 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"
}
});
However, when I do the same with IReadOnlyCollection
(or IReadOnlyList
):
[ProtoContract(ImplicitFields = ImplicitFields.AllPublic)]
public class Sheep
{
public IReadOnlyCollection<string> Children { get; set; }
public Sheep()
{
Children = new List<string>();
}
}
I get an exception:
Unable to resolve a suitable Add method for System.Collections.Generic.IReadOnlyCollection`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
So, is there a way to make protobuf-net handle members of IReadOnlyCollection
/IReadOnlyList
?
EDIT: I've opened an issue for this feature on the project's repository: Support IReadOnlyCollection/IReadOnlyList members
Upvotes: 4
Views: 1816
Reputation: 1063824
The main problem here is "merge". For non-merge (or basically: with list replacement enabled), we could probably hard-code a concrete type and strategy, however, there is no obvious thing to do for merge otherwise.
There is no magic for this scenario at the moment; any solution would require library changes.
Upvotes: 3