【问题标题】:Can I make sure resharper does not change the order of the fields in this class?我可以确保 resharper 不会更改此类中字段的顺序吗?
【发布时间】:2019-04-03 13:31:55
【问题描述】:

我正在使用this NetResource class 将文件发送到网络驱动器,它看起来像这样:

[StructLayout(LayoutKind.Sequential)]
public class NetResource
{
    public ResourceScope Scope;
    public ResourceType ResourceType;
    public ResourceDisplayType DisplayType;
    public int Usage;
    public string LocalName;
    public string RemoteName;
    public string Comment;
    public string Provider;
}

现在,这些字段的顺序保持不变非常重要,正如 StructLayout 属性所暗示的那样。

但是,当有人运行 resharper 清理时,resharper 决定移动字段,这会破坏代码。

有什么方法可以告诉 rehsarper 不要乱用它吗?我觉得如果我不能做到这一点,那么最终有人会破坏代码并且不知道去哪里找。

但我认为一个平庸的解决方案是创建一个可以检查布局是否符合预期的单元测试。

编辑:我见过this answer,但它已经过时了,需要更新resharper 设置。我也不能保证同事使用这种重新锐化器设置。我正在寻找一种在代码中添加它的方法,就像你可以这样做// ReSharper disable once InconsistentNaming

【问题讨论】:

标签: resharper structlayout


【解决方案1】:

我在这里看到了几个解决方案:

  • 您可以使用NoReorderAttribute 中的JetBrains.Annotations 标记该类(有多种方法可以将它们添加到项目中)。然后 ReSharper 将停止对标记代码实体内的成员重新排序。
  • 主要是关于already mentioned answer,我将向您展示如何在上一个 ReSharper 构建中获得相同的东西。您只需在 ReSharper | 中将“System.Runtime.InteropServices.StructLayoutAttribute”添加到“Non-reorderable types”模式即可。选项 |代码编辑 | C# |文件布局。

第 1 步:

第二步:

第三步:

第四步:

第五步:

为确保您的同事在 ReSharper 中使用相同的设置,请将此更改保存到 Solution team shared layer(选项对话框底部的Save To)。然后,如果您的任何同事打开解决方案,ReSharper 将自动使用该层的设置,无需额外操作。

【讨论】:

  • 不过,这是known bug 的解决方法,不是吗?
猜你喜欢
  • 2014-09-20
  • 1970-01-01
  • 2012-12-16
  • 1970-01-01
  • 2014-12-17
  • 1970-01-01
  • 1970-01-01
  • 2022-07-11
  • 1970-01-01
相关资源
最近更新 更多