gpt4 book ai didi

c# - 使用 ReSharper 为属性生成只读字段

转载 作者:太空狗 更新时间:2023-10-29 22:26:41 26 4
gpt4 key购买 nike

由于我没有发现 ReSharper 是否能够为属性生成私有(private)只读支持字段,有没有办法定义可以做到这一点的自定义脚本?

假设你有一个属性(property)

public Type PropertyName { get; set; }

然后在这一行上设置一个光标,按 Alt+Enter 就会出现一个将属性转换为的菜单项

private readonly Type propertyName;

public Type PropertyName { get { return propertyName; } }

我发现自己实际上会多次使用它。

最佳答案

使用 R#,您可以创建 Live Template .在这种情况下:

  1. ReSharper > 模板资源管理器
  2. 实时模板选项卡 > 新模板(小网格图标)
  3. 快捷方式:propReadOnly(这将是与智能感知一起使用的快捷方式)
  4. 描述:任何适合你的...
  5. 可用:在 C# 2.0 中允许类型成员声明
  6. 将模板创建为:

    private readonly $Type$ $propertyName$;

    public $Type$ $PropertyName$ { get { return $propertyName$; } }

$SomeText$ 中包含文本将指定该文本作为 R# 进行操作的变量。

在变量名面板中:

Name             Value             Editable Occurence
Type Choose Macro* #2
PropertyName Choose Macro* checkbox is checked
propertyName (see below) not editable
Macro > - Value of annother variable with the first character in lower case
- Select "PropertyName" as the other variable

*Choose Macro is displayed when no macro is selected; this is the default setting

保存,您可以通过键入使用的快捷方式(即“propReadOnly”)立即在 Visual Studio 中使用该模板

关于c# - 使用 ReSharper 为属性生成只读字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19730793/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com