Is there any way to use IReadOnlyCollection <T> / IReadOnlyList <T> with protobuf-net
I understand that when working with collections, protobuf-net requires GetEnumeratorfor serialization and type c Addfor deserialization.
For types that do not have Add, you can set the type of inheritance that runs before deserialization. This works, for example, with IEnumerableand 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:
Cannot resolve the appropriate Add method to System.Collections.Generic.IReadOnlyCollection`1 [[System.String, mscorlib, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089]]
So, is there a way to make the elements of a protobuf-net IReadOnlyCollection/ element IReadOnlyList?
+4
1