在 VS2013 中为 C# 创建自定义代码片段

Creating Custom Code Snippet in VS2013 for C#

Creating Custom Code Snippet 对我帮助不大。我的问题是针对我的要求的。

我想为我的 属性 编写自定义代码段。这种情况通常是当我们写 prop 和双击我们会得到输出

public int MyProperty { get; set; }

当我们写 propfull 时,我们得到

private int myVar;

public int MyProperty
{
    get { return myVar;}
    set { myVar = value;}
}

只要我们更改变量名,它就会自动反映到所有地方

现在我想像这样写我自己的片段

public int MyProperty
{
    get
    {
        return GetValue(() => MyProperty);
    }
    set
    {
        SetValue(() => MyProperty, value);
    }
}

我从 MSDN

获得了 Creating a Code Snippet

这是我试过的

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>propmy</Title>
        </Header>
        <Snippet>
            <Code Language="csharp"><![CDATA[public int MyProperty
        {
                get { return GetValue(() => MyProperty); }
                set { SetValue(() => MyProperty , value); }
        }
$end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

但是当我在 VS IDE 中编写 propmy 时,列表中没有显示任何内容,它在第一个选项卡和第二个选项卡中转为 prop,它像正常一样创建 属性。我不知道如何进行?

您可以尝试添加

<Shortcut>propmy</Shortcut>

在 XML 的 header 部分。我相信这会成功

编辑:

我已经为您创建了完整的 XML。只需复制粘贴即可。

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>propmy</Title>
            <Shortcut>propmy</Shortcut>
            <Description>Automatically implemented property</Description>
            <Author>BugFree</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>type</ID>
                    <ToolTip>Property type</ToolTip>
                    <Default>int</Default>
                </Literal>
                <Literal>
                    <ID>property</ID>
                    <ToolTip>Property name</ToolTip>
                    <Default>MyProperty</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp"><![CDATA[public $type$ $property$
        {
                get { return GetValue(() => $property$); }
                set { SetValue(() => $property$ , value); }
        }
$end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>