简体   繁体   中英

Make a Property Code Snippet in Visual Studio

I got tired to write boiler plate properties code such as:

public string Name
{
     get { return this.name; }
     set { SetProperty(ref name, value); }
}

So I decided to make a code snippet in Visual Studio to automate the process:

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

the problem is, I wanted to just type the name once, and have it as "name" in the field and as "Name" in the property, with the uppercase. Also, I wanted to automatically group the backing fields in the beginning of the class, just as the good practices recommend.

Does anyone knows how to do this?

I can't comment so I will answer. What about:

public string Name { get; set; }

you have no backing variables and the properties can be defined in <type,name> pair

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM