Resharper doesn't automatically convert to auto properties in Serializable classes - should I?
Asked Answered
F

1

12

I ran across this issue today and was able to determine that, when doing code cleanup, R# will not convert properties from having backing fields to auto properties in classes that are decorated with the SerializableAttribute, e.g.

using System; 

namespace DataContracts
{
    [Serializable]
    public class Class1
    {
        private bool _wontChange;

        public bool WontChange
        {
            get { return _wontChange; }
            set { _wontChange = value; }
        }
    }
}

The above code will not be changed during automatic code cleanup. Of course, I can do this manually, and I still get the quick-action menu option from R# to do it at the individual property level. But it's got me wondering if there's an underlying issue that I'm not aware of in using auto properties in [Serializable] classes.

In the JetBrains forum thread we're referred to an issue in which this problem is discussed, but it does not seem to be definitively resolved.

Fimbriation answered 12/8, 2010 at 16:45 Comment(1)
You say that you can use the "quick-action menu" to do the job. The option is just not there. And when I remove the serializable attribute, everything is ok.Edla
H
14

When you serialize objects the field-names etc start to matter, because most serialization-mechanism use the field-names to restore the serialized data. Now when you change the field-name you cannot read older serialized versions correctly.

When you convert to auto-properties, the backing-field will have a auto-generated name, which doesn't match the old name. Therefore this would introduce a potential problem when reading old serialized data.

I assume to avoid this pitfall, R# doesn't change it automatically to a auto-property if the class is marked as serializable.

Holusbolus answered 12/8, 2010 at 19:15 Comment(3)
I presume that this would not be an issue with objects that were serialized and deserialized from the same DLL - the backing fields would be statically defined within the DLL. Is that a valid assumption?Fimbriation
I have never run into this issue described with auto properties and serialization. I have read the R# bug youtrack.jetbrains.com/issue/RSRP-63531 in it and this SO there is not evidence provided show this in action? Where is the reproducible project? Was this an issue back in CLR1 and CLR2 days was it fixed in a later CLR 2 update or in CLR 4?Lindeman
Thank you so much. It would be nice to have this type of functionality documented on their website.Edla

© 2022 - 2024 — McMap. All rights reserved.