格式化C#代码段的文字参数

Mic*_*ang 30 c# code-generation code-snippets

有什么方法可以改变代码片段的文字在代码片段生成的代码中使用时的呈现方式吗?

具体来说,我想知道我是否可以有一个名为say,$ PropertyName $的字面值,然后让代码片段引擎呈现"_ $ PropertyName $,其中第一个字符是小写的.

我买不起R#.请帮忙 :)

Cae*_*nog 21

不幸的是似乎没有办法.如您所见,片段为转换功能提供了极其有限的支持.

您必须坚持使用VS标准解决方案,即编写两个文字:一个用于属性名称,另一个用于成员变量名称.

  • [从Visual Studio 2013开始](http://msdn.microsoft.com/en-us/library/ms242312(v = vs.120).aspx),三个可用的函数仍然只有:`GenerateSwitchCases`,`ClassName `,和`SimpleTypeName`. (3认同)
  • 恐怕我还没有。我最终购买了 R# 的个人许可证 (2认同)

小智 5

“修复”可能是在命名或成员变量中使用前缀,即:

string m_$name$;
string $name$
{
 get{return m_$name$;}
 set{m_$name$=value;}
};
Run Code Online (Sandbox Code Playgroud)

  • 是的,但是不允许您更改$ name $的大小写以使其为camelCase。(通常,人们使用“ _property”作为属性“ Property”等的后盾,等等。而且,m_”也已被弃用……因为像vb6 ...?请不要使用它)。 (4认同)

Nik*_*nin 5

您可以输入大写的首字母,然后是属性名称,然后是小写的首字母。试试这个片段:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <Header>
    <Title>Notifiable Property</Title>
    <Author>Nikolay Makhonin</Author>
    <Shortcut>propn</Shortcut>
    <Description>Property With in Built Property Changed method implementation.</Description>
    <SnippetTypes>
      <SnippetType>SurroundsWith</SnippetType>
      <SnippetType>Expansion</SnippetType>
    </SnippetTypes>
  </Header>
  <Snippet>
    <Declarations>
        <Literal>
            <ID>Type</ID>
            <Default>Type</Default>
        </Literal>
        <Literal>
            <ID>P</ID>
            <Default>P</Default>
        </Literal>
        <Literal>
            <ID>roperty</ID>
            <Default>ropertyName</Default>
        </Literal>
        <Literal>
            <ID>p</ID>
            <Default>p</Default>
        </Literal>
        <Literal>
            <ID>Ownerclass</ID>
            <ToolTip>The owning class of this Property.</ToolTip>
            <Function>ClassName()</Function>
            <Default>Ownerclass</Default>
        </Literal>
    </Declarations>
    <Code Language="CSharp">
      <![CDATA[#region $P$$roperty$

        private Field<$Type$> _$p$$roperty$;
        public static readonly string $P$$roperty$PropertyName = GetPropertyName(() => (($Ownerclass$)null).$P$$roperty$);
        public $Type$ $P$$roperty$
        {
            get { return _$p$$roperty$; }
            set { Set(ref _$p$$roperty$, value); }
        }

        #endregion

]]>
    </Code>
  </Snippet>
</CodeSnippet>
Run Code Online (Sandbox Code Playgroud)