5

Can protobuf-net handle the new auto read-only properties, i.e. auto properties defined with a single get and no private set?

public class WithReadonlyProperty {
    public int ReadonlyProperty { get; }
    public WithReadonlyProperty(int val) {
        ReadonlyProperty = val;
    }
}

When I do this

RuntimeTypeModel
    .Default
    .Add(typeof (WithReadonlyProperty), false)
        .Add(nameof(WithReadonlyProperty.ReadonlyProperty));
var test = new WithReadonlyProperty(12345);
using (var output = File.Create(@"c:\temp\_readonly.bin")) {
    try {
        Serializer.Serialize(output, test);
    } catch (Exception e) {
        Console.WriteLine(e);
    }
}

I get this exception:

System.InvalidOperationException: Cannot apply changes to property WithReadonlyProperty.ReadonlyProperty
   at ProtoBuf.Serializers.PropertyDecorator.SanityCheck(TypeModel model, PropertyInfo property, IProtoSerializer tail, Boolean& writeValue, Boolean nonPublic, Boolean allowInternal) in c:\Dev\protobuf-net\protobuf-net\Serializers\PropertyDecorator.cs:line 46
   at ProtoBuf.Serializers.PropertyDecorator..ctor(TypeModel model, Type forType, PropertyInfo property, IProtoSerializer tail) in c:\Dev\protobuf-net\protobuf-net\Serializers\PropertyDecorator.cs:line 32
   at ProtoBuf.Meta.ValueMember.BuildSerializer() in c:\Dev\protobuf-net\protobuf-net\Meta\ValueMember.cs:line 375
   at ProtoBuf.Meta.MetaType.BuildSerializer() in c:\Dev\protobuf-net\protobuf-net\Meta\MetaType.cs:line 408
   at ProtoBuf.Meta.MetaType.get_Serializer() in c:\Dev\protobuf-net\protobuf-net\Meta\MetaType.cs:line 384
   at ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest) in c:\Dev\protobuf-net\protobuf-net\Meta\RuntimeTypeModel.cs:line 752
   at ProtoBuf.Meta.TypeModel.SerializeCore(ProtoWriter writer, Object value) in c:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 186
   at ProtoBuf.Meta.TypeModel.Serialize(Stream dest, Object value, SerializationContext context) in c:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 217
   at ProtoBuf.Serializer.Serialize[T](Stream destination, T instance) in c:\Dev\protobuf-net\protobuf-net\Serializer.cs:line 86
    ...

Is it possible to configure protobuf-net to use the public constructor? Is there perhaps some other way to do it? I would like to avoid decorating the WithReadonlyProperty class with attributes, if possible.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523

1 Answers1

0

Protobuf can use private constructors, so perhaps you can make private constructors for protobuf to use?

Giovanni Galbo
  • 12,963
  • 13
  • 59
  • 78
  • I have a public constructor there, shouldn't it be even better than a private one? – Sergey Kalinichenko Aug 19 '15 at 13:59
  • 1
    You'd make the parameterless constructor private. But this would require private setters, so I guess the answer would be "no" to being able to use read only properties, since private set is not the same as readonly. – Giovanni Galbo Aug 19 '15 at 14:00