I’ve got an interface and an implementation, both with [ProtoContract]. There is one property, which is read-only on the interface. When I try to deserialize a property declared as the interface, Protobuf gives me this error:
System.InvalidOperationException: Cannot apply changes to property IFoo.Id
Here’s the code I’m testing with:
public void Main()
{
using (var ms = new MemoryStream())
{
var x = new HasFoo {TheFoo = new Foo(1)};
Serializer.Serialize(ms, x);
ms.Position = 0;
var clone = Serializer.Deserialize<HasFoo>(ms);
Assert.AreEqual(1, clone.TheFoo.Id);
}
}
[ProtoContract, ProtoInclude(100, typeof(Foo))]
public interface IFoo
{
[ProtoMember(1)]
long Id { get; }
}
[ProtoContract]
public class Foo : IFoo
{
[ProtoMember(1)]
public long Id { get; private set; }
public Foo() { }
public Foo(long id)
{
Id = id;
}
}
[ProtoContract]
public class HasFoo
{
[ProtoMember(1)]
public IFoo TheFoo { get; set; }
}
I’d rather not declare a setter on the interface, and I’d like to keep the TheFoo property declared as IFoo if at all possible. Is there any way that can be made to work? I’m using protobuf-net v2.
No. When binding to an interface, it binds to the interface, so it can’t exploit any private setters (etc) that would be available when binding to the concrete type. The only way you could do that would be to advertise the members of Foo for serialization instead of IFoo.
Note: in non-interface scenarios it will have access to the members, or even the direct field of desired.