andyQn / protobuf-net

Automatically exported from code.google.com/p/protobuf-net
Other
0 stars 0 forks source link

Error with readonly properties #383

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Please include an e-mail address if this might need a dialogue!
lukemauldin AT gmail DOT com

What steps will reproduce the problem?
1. Download the latest version of protobuf-net
2. Create a transport object with a readonly property:

  [DataContract]
    public sealed class ClaimTouchpointRecord
    {
        [DataMember(Order = 1)]
        public int TouchpointId { get; set; }

        [DataMember(Order = 2)]
        public int ClaimNumber { get; set; }

        [DataMember(Order = 3)]
        public string Application { get; set; }

        [DataMember(Order = 4)]
        public string User { get; set; }

        public DateTime Date { get; set; }

        [DataMember(Order = 5)]
        public long DateUnix
        {
            get
            {
                return DateCalc.ToUnixTime(this.Date);
            } 
        //    set
         //   {
          //      throw new NotImplementedException();
          //  }
        }
}

3. Attempt to serialize the class above using protobuf-net

What is the expected output? What do you see instead?
I receive this error:  Cannot apply changes to property 
MRA.Touchpoint.WebService.Domain.ClaimTouchpointRecord.DateUnix

If I uncomment the code above and provide a setter, then the serialization 
works as expected.

Original issue reported on code.google.com by lukemaul...@gmail.com on 29 May 2013 at 1:30

GoogleCodeExporter commented 8 years ago
I haven't tried this, but you could try:

        [DataMember(Order = 5)]
        public long DateUnix
        {
            get
            {
                return DateCalc.ToUnixTime(this.Date);
            }
            private set;
        }

But in general you're going to need a setter in order for serialization to have 
something to set when deserializing.

Original comment by xeri...@gmail.com on 3 Jun 2013 at 10:32

GoogleCodeExporter commented 8 years ago
As xeridon nodes, it needs some mechanism to try to set the value during 
deserialization. Since you clearly want `DateUnix` to *work*, it doesn't want 
to disappoint you. You can usually get away with something like:

    private set {
        this.Date = DateCalc.FromUnixTime(value);
    }

or, if you want to simply discard the result you could have an empty setter - 
not sure that would be useful, though:

    private set {}

Original comment by marc.gravell on 4 Jun 2013 at 6:54