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.