views:

67

answers:

1

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.

+2  A: 

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.

Gamlor
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?
arootbeer