Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 625
Matt Mills Avatar asked Aug 12 '10 16:08

Matt Mills


1 Answers

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.

like image 154
Gamlor Avatar answered Sep 28 '22 05:09

Gamlor