Resharper 不会自动转换为可序列化类中的自动属性 ​​- 应该吗?

发布于 2024-09-14 13:39:53 字数 728 浏览 4 评论 0原文

我今天遇到了这个问题,并能够确定,在进行代码清理时,R# 不会将具有支持字段的属性转换为使用 SerializedAttribute 装饰的类中的自动属性,例如,

using System; 

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

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

上面的代码在自动过程中不会更改代码清理。当然,我可以手动执行此操作,并且我仍然可以从 R# 获得快速操作菜单选项以在单个属性级别执行此操作。但这让我想知道在 [Serialized] 类中使用自动属性是否存在我不知道的潜在问题。

JetBrains 论坛帖子中,我们提到了一个问题,其中讨论了此问题,但似乎并未得到明确解决。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清秋悲枫 2024-09-21 13:39:53

当您序列化对象时,字段名称等开始变得重要,因为大多数序列化机制使用字段名称来恢复序列化数据。现在,当您更改字段名称时,您无法正确读取旧的序列化版本。

当您转换为自动属性时,支持字段将具有自动生成的名称,该名称与旧名称不匹配。因此,在读取旧的序列化数据时,这会带来潜在的问题。

我认为为了避免这个陷阱,如果类被标记为可序列化,R# 不会自动将其更改为自动属性。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文