开发者

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 do开发者_如何学JAVAes not seem to be definitively resolved.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜