如何在 VS2008 代码片段中强制使用大写?

发布于 2024-08-20 18:49:31 字数 1878 浏览 7 评论 0原文

我决定采用以下创建属性的风格(带有支持字段):

private _firstName;
public string FirstName
{
    get { return _firstName; }
    set { _firstName = value; }
}

鉴于属性的名称与支持字段的名称相似,我改进了内置的 prop片段如下:

<?xml version="1.0"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>prop</Title>
      <Author>Frank Rizzo</Author>
      <Description>Code snippet for property and backing field - changed one (not the original).</Description>
      <Shortcut>prop</Shortcut>
      <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>
        <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$ $field$
    {
        get { return _$field$;}
        set { _$field$ = value;}
    }
    $end$
            ]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

这在一定程度上有效,但支持字段和属性名称的大小写始终相同,按照我的约定,支持字段是驼峰式大小写,而属性名称是帕斯卡大小写。

所以我的问题是:代码片段语法是否有办法更改属性的第一个字母,以便代码片段符合我的约定?

I've settled on the following style of creating properties (with a backing field):

private _firstName;
public string FirstName
{
    get { return _firstName; }
    set { _firstName = value; }
}

Given that the name of the property is similar to the name of the backing field, I've improved the built in prop snippet to the following:

<?xml version="1.0"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>prop</Title>
      <Author>Frank Rizzo</Author>
      <Description>Code snippet for property and backing field - changed one (not the original).</Description>
      <Shortcut>prop</Shortcut>
      <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>
        <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$ $field$
    {
        get { return _$field$;}
        set { _$field$ = value;}
    }
    $end$
            ]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

This works to a degree, but the casing of the backing field and the property name is always the same, where as in my convention, the backing field is camel-cased, where as the property name is pascal-cased.

So my question is this: does the snippet syntax have a way to change the first letter of the property, so that the snippet might comply with my convention?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随心而道 2024-08-27 18:49:31

不幸的是,这是不可能的(目前)。

希望 Visual Studio 的未来版本能够实现这一点。

Unfortunately, this is not possible (yet).

Hopefully, it will be possible in a future version of Visual Studio.

入画浅相思 2024-08-27 18:49:31
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <Header>
    <Title>Code File Header</Title>
    <Author>Муся</Author>
    <Shortcut>codehead</Shortcut>
    <Description>Гавнокодец</Description>
    <SnippetTypes>
      <SnippetType>Expansion</SnippetType>
    </SnippetTypes>
  </Header>
  <Snippet>
    <Declarations>
      <Literal>
        <ID>Name</ID>
        <Default>Header</Default>
      </Literal>
      <Literal>
        <ID>LowerName</ID>
        <Default>header</Default>
      </Literal>
      <Literal>
        <ID>Type</ID>
        <Default>object</Default>
      </Literal>
    </Declarations>
    <Code Language="CSharp">
      <![CDATA[
        public $Type$ $Name$
        {
            get { return $LowerName$; }
            set
            {
                $LowerName$ = value;
                OnPropertyChanged("$Name$");
            }
        }
        $Type$ $LowerName$;
      ]]>
    </Code>
  </Snippet>
</CodeSnippet>
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <Header>
    <Title>Code File Header</Title>
    <Author>Муся</Author>
    <Shortcut>codehead</Shortcut>
    <Description>Гавнокодец</Description>
    <SnippetTypes>
      <SnippetType>Expansion</SnippetType>
    </SnippetTypes>
  </Header>
  <Snippet>
    <Declarations>
      <Literal>
        <ID>Name</ID>
        <Default>Header</Default>
      </Literal>
      <Literal>
        <ID>LowerName</ID>
        <Default>header</Default>
      </Literal>
      <Literal>
        <ID>Type</ID>
        <Default>object</Default>
      </Literal>
    </Declarations>
    <Code Language="CSharp">
      <![CDATA[
        public $Type$ $Name$
        {
            get { return $LowerName$; }
            set
            {
                $LowerName$ = value;
                OnPropertyChanged("$Name$");
            }
        }
        $Type$ $LowerName$;
      ]]>
    </Code>
  </Snippet>
</CodeSnippet>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文