【问题标题】:How to lower case a Visual Studio Code Snippet variable? [duplicate]如何小写 Visual Studio 代码片段变量? [复制]
【发布时间】:2014-03-27 17:38:33
【问题描述】:

我已经构建了一些 sn-ps 来为设置类生成一个字段。我现在使用 2 个变量 - $setting$$Setting$ - 为属性和支持字段生成名称。我喜欢使用单个变量,因为唯一的区别是支持字段始终是小写版本。

当前代码:

string $setting$;

/// <summary>
/// Gets the $bigComment$.
/// </summary>
/// <value>The $smallComment$.</value>
public string $Setting$
{
    get
    {
        if (String.IsNullOrEmpty($setting$))
        {
            $setting$ = CFW.Common.Configuration.GetAppSetting("$Setting$", $defaultValue$);
        }

        return $setting$;
    }
}

这可能吗?

【问题讨论】:

    标签: c# .net visual-studio code-snippets


    【解决方案1】:

    根据the official MSDN docs,sn-p 变量的默认值应该在 sn-p 的 XML 中定义,而不是使用变量名。

    所以你会有这样的东西:

    <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/CodeSnippet">
        <CodeSnippet>
            <Header>
                <!-- Add header information here -->
            </Header>
            <Snippet>
                <!-- Add additional Snippet information here -->
                <Declarations>
                    <Literal>
                        <ID>SettingsField</ID>
                        <ToolTip>The settings field.</ToolTip>
                        <Default>settings</Default>
                    </Literal>
                    <Object>
                        <ID>SettingsProperty</ID>
                        <ToolTip>The settings property.</ToolTip>
                        <Default>Settings</Default>
                    </Object>
                </Declarations>
                <Code Language="CSharp">
                    <![CDATA[
                        Snippet code with $SettingsField$ and $SettingsProperty$
                    ]]>
                </Code>
            </Snippet>
        </CodeSnippet>
    </CodeSnippets>
    

    【讨论】:

    • 是的,我知道。问题是,我想为 $SettingsField$ 和 $SettingsProperty$ 使用一个文字 - 因为唯一不同的是第一个字母的大小写。
    • 如果您希望 Visual Studio 自动将一个变量设为另一个变量的 camelCase 版本,恐怕这是不可能的。否则......生成的代码会是什么样子?不同的成员不能有相同的名字。
    • @SolalPirelli 有很多例子,你想要骆驼大小写的版本。
    【解决方案2】:

    无法更改代码片段中的文字。有一些功能可用:

    GenerateSwitchCases - 为 EnumerationLiteral 参数指定的枚举成员生成一个 switch 语句和一组 case 语句。 EnumerationLiteral 参数必须是对枚举文字或枚举类型的引用。

    ClassName - 返回包含插入的 sn-p 的类的名称。

    SimpleTypeName - 在调用 sn-p 的上下文中将 TypeName 参数简化为最简单的形式。

    但他们不能修改文字。

    来源:http://msdn.microsoft.com/en-us/library/ms242312(v=vs.110).aspx

    【讨论】:

      猜你喜欢
      • 2021-05-29
      • 2020-09-07
      • 1970-01-01
      • 2018-07-07
      • 2016-11-17
      • 2011-07-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多