【问题标题】:Visual Studio 2017 Avoid Lambdas When Generating PropertiesVisual Studio 2017 在生成属性时避免使用 Lambda
【发布时间】:2017-12-06 02:35:29
【问题描述】:

假设我在课堂上有私人领域。如果我告诉 Visual Studio 用属性封装该字段,它会为 get 和 set 访问器输出 lambda 表达式。

namespace MyNamespace 
{
  public class MyClass
  {  
    private bool isActive;

    //Auto-Generated Property
    public bool IsActive
    {
      get => isActive;
      set => isActive = value;
    }
  }
}

但我宁愿为每个访问器配备一对大括号。

namespace MyNamespace 
{
  public class MyClass
  {  
    private bool isActive;

    //Auto-Generated Property
    public bool IsActive
    {
      get 
      {
        return isActive;
      }
      set 
      {
        isActive = value;
      }
    }
  }
}

我怎样才能改变这种行为?我知道这里存在 sn-ps: “C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC#\Snippets\1033\Refactoring” 但我不知道如何改变它们以获得我想要的行为。

【问题讨论】:

  • 这些不是 lambda。这只是一种用于声明属性的 getter 和 setter 的花哨的新语法。这很好。你应该想要它。
  • 除了 VS 2017 是版本“15”。这些是错误的 sn-ps。
  • 为什么还要有私有字段?让编译器完成工作:)
  • @MikeNakis 但是,如果我想在我的私有字段中更新值后调用我的设置器中的方法 - 比如“NotifyPropertyChanged()”?我不明白如何使用新语法来做到这一点。
  • @Mike z 我真傻。我知道有些事情没有加起来。

标签: c# visual-studio-2017


【解决方案1】:

这就是我一直在寻找的 - propfull 快捷方式。有趣的是,如果您已经有一个私有字段,Visual Studio 会生成一个与您从头开始的情况相比具有不同(较新)语法的公共属性。

Shortcut to create properties in Visual Studio?

我使用这个代码 sn-p 创建了我自己的代码 sn-p,我在其中创建了一个带有支持字段的公共属性。但是对于公共属性,我添加了一个 RaisedPropertyChanged() 方法调用,因为我正在使用 WPF 执行 MVVM,并且我想使用此快捷方式轻松地在 ObservableObjects 上创建属性。我的问题并不清楚我的最终目标是什么。

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>obsprop</Title>
            <Shortcut>obsprop</Shortcut>
            <Description>Code snippet for property and backing field for observable object</Description>
            <Author>Microsoft Corporation</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>type</ID>
                    <ToolTip>Property type</ToolTip>
                    <Default>string</Default>
                </Literal>
                <Literal>
                    <ID>property</ID>
                    <ToolTip>Property name</ToolTip>
                    <Default>MyProperty</Default>
                </Literal>
                <Literal>
                    <ID>field</ID>
                    <ToolTip>The variable backing this property</ToolTip>
                    <Default>myVar</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp"><![CDATA[private $type$ $field$;
    public $type$ $property$
    {
        get { return $field$;}
        set 
        { 
            $field$ = value;
            RaisePropertyChanged();
        }
    }
    $end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多