How to: Specify a Language in Text Templates

You can add a templatedirective to text templates. A template directive enables you to specify the language that you want to use in your text template. For more information, see Directive Syntax (Domain-Specific Languages).

You can specify Visual C# or Visual Basic. The language that you specify declares the programming language used for code statements, expressions, and class features inside your text template.

注意

The generated text output that the transformation process creates can be in any language, not necessarily the same language in which the text template is written. (The output language does not even have to be a programming language.)

To specify the language to use in a template

  1. In Solution Explorer, right-click the text template file that you want to specify a language for, and then click Open.

    The template opens in the text editor.

    At the top of the file, add a template directive, specifying VB for Visual Basic or C# for Visual C#. If your text template already contains a template directive, add the language parameter to the existing one. If the template directive contains multiple parameters, the order does not matter. You can add the language parameter to the beginning or the end of the template directive.

    <#@ template language="VB" debug="true" #>
    
    <#@ template debug="true" language="C#"#>
    

    注意

    If you omit the language parameter, the default language is Visual C#.

  2. Save the file.

    注意

    To debug text templates, you must set the debug parameter of the template directive. For more information, see How to: Debug Text Templates.

Security

For more information, see Security of Text Templates.

See Also

Concepts

Using Built-in Directives in Text Templates

Adding Code to Text Templates

Generating Artifacts Using Text Templates

Walkthrough: Creating and Running Text Templates