Visual Studio 2017 在生成属性时避免使用 lambda



>假设我在类中有私有字段。如果我告诉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;
}
}
}
}

如何更改行为?我知道片段存在在这里: "C:\Program Files (x86(\Microsoft Visual Studio 14.0\VC#\Snippets\1033\Refactoring" 但是我不知道如何改变它们以获得我想要的行为。

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

在Visual Studio中创建属性的快捷方式?

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

<?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>

最新更新