在 Visual Studio 中制作属性代码片段

mFe*_*ein 5 c# properties code-snippets visual-studio-2012 visual-studio-2013

我厌倦了编写样板属性代码,例如:

public string Name
{
     get { return this.name; }
     set { SetProperty(ref name, value); }
}
Run Code Online (Sandbox Code Playgroud)

因此,我决定在 Visual Studio 中制作一个代码片段来自动化该过程:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>MVVM Property</Title>
      <Shortcut>propm</Shortcut>
      <Author>MFeinstein</Author>
      <Description>Adds a Property that calls PRISM no Notify any changes</Description>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>type</ID>
          <ToolTip>Replace with the property type</ToolTip>
          <Default>string</Default>
        </Literal>
        <Literal>
          <ID>PropertyName</ID>
          <ToolTip>Replace with the property name</ToolTip>
          <Default>propertyName</Default>
        </Literal>
        <Literal>
          <ID>fieldName</ID>
          <ToolTip>Replace with the field name</ToolTip>
          <Default>fieldName</Default>
        </Literal>
      </Declarations>
      <Code Language="csharp">
        <![CDATA[private $type$ $fieldName$;

        public $type$ $PropertyName$
        {
            get { return this.$fieldName$; }
            set { SetProperty(ref $fieldName$, value); }
        }$selected$ $end$]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>
Run Code Online (Sandbox Code Playgroud)

问题是,我只想输入一次名称,并将其作为字段中的“名称”和属性中的“名称”,使用大写。另外,我想在课程开始时自动对支持字段进行分组,就像良好实践所建议的那样。

有谁知道该怎么做?

Ste*_*eve -1

我无法发表评论,所以我会回答。关于什么:

public string Name { get; set; }

您没有支持变量,并且属性可以成对<type,name>定义