文本模板实用工具方法

在 Visual Studio 文本模板中写入代码时,这几种方法始终可用。 在 TextTransformation 中定义这些方法。

提示

还可以在常规(未预处理)文本模板中使用主机环境提供的其他方法和服务。 例如,可以解决文件路径、记录错误并获取 Visual Studio 和任意已加载包提供的服务。 有关详细信息,请参阅从文本模板访问 Visual Studio

写入方法

可以使用 Write()WriteLine() 方法在标准代码块内追加文本,而不是使用表达式代码块。 以下两个代码块功能一样。

包含表达式块的代码块

<#
int i = 10;
while (i-- > 0)
    { #>
        <#= i #>
    <# }
#>

使用 WriteLine() 的代码块

<#
    int i = 10;
    while (i-- > 0)
    {
        WriteLine((i.ToString()));
    }
#>

你会发现使用其中一个这些实用工具方法(而不是长代码块中具有嵌套控件结构的表达式块)会很有帮助。

Write()WriteLine() 方法具有两个加载,一个采用单个字符串参数,另一个采用组合格式字符串和要包含在字符串中的对象阵列(如 Console.WriteLine() 方法)。 WriteLine() 的以下两种用法功能一样:

<#
    string msg = "Say: {0}, {1}, {2}";
    string s1 = "hello";
    string s2 = "goodbye";
    string s3 = "farewell";

    WriteLine(msg, s1, s2, s3);
    WriteLine("Say: hello, goodbye, farewell");
#>

缩进方法

可以使用缩进方法设置文本模板的输出格式。 TextTransformation 类具有 CurrentIndent 字符串属性,可以显示文本模板中的当前缩进,还具有 indentLengths 字段,其中包含已添加的缩进列表。 可以使用 PushIndent() 方法添加缩进,使用 PopIndent() 方法减去缩进。 如果要删除所有缩进,请使用 ClearIndent() 方法。 以下代码块演示这些方法的用法:

<#
    WriteLine(CurrentIndent + "Hello");
    PushIndent("    ");
    WriteLine(CurrentIndent + "Hello");
    PushIndent("    ");
    WriteLine(CurrentIndent + "Hello");
    ClearIndent();
    WriteLine(CurrentIndent + "Hello");
    PushIndent("    ");
    WriteLine(CurrentIndent + "Hello");
#>

此代码生成以下输出:

Hello
        Hello
                Hello
Hello
        Hello

错误和警告方法

可以使用错误和警告实用工具方法将消息添加到 Visual Studio 错误列表中。 例如,以下代码将错误消息添加到错误列表。

<#
  try
  {
    string str = null;
    Write(str.Length.ToString());
  }
  catch (Exception e)
  {
    Error(e.Message);
  }
#>

对主机和服务提供商的访问

属性 this.Host 可以提供对由正在执行模板的主机公开的属性的访问。 若要使用 this.Host,必须在 <@template#> 指令中设置 hostspecific 属性:

<#@template ... hostspecific="true" #>

this.Host 的类型取决于正在其中执行模板的主机类型。 在 Visual Studio 中运行的模板中,可以将 this.Host 强制转换为 IServiceProvider,以获取对服务(如 IDE)的访问。 例如:

EnvDTE.DTE dte = (EnvDTE.DTE) ((IServiceProvider) this.Host)
                       .GetService(typeof(EnvDTE.DTE));

使用一组不同的实用工具方法

作为文本生成过程的一部分,模板文件将转换为类,该类始终命名为 GeneratedTextTransformation,并且继承自 TextTransformation。 如果要改为使用一组不同的方法,则可以编写自己的类,并在模板指令中指定它。 类必须从 TextTransformation 继承。

<#@ template inherits="MyUtilityClass" #>

使用 assembly 指令引用可在其中找到已编译类的程序集。