How to: Generate Code from Text Templates

You can use text templates to generate code output. For your code output, you must choose either Visual C# or Visual Basic. To generate code files, you must include valid code blocks. Also, you must ensure that any expressions and write statements generate valid code.

To generate code output from a text template

  • Under the directives in your existing code, type your code.

    The following example shows how to generate code in Visual C#.

    <#@ template inherits="Microsoft.VisualStudio.TextTemplating.VSHost.ModelingTextTransformation"#>
    <#@ MyLanguage processor="<YourLanguageName>DirectiveProcessor" requires="fileName='Sample.mmm'"#>
    <#@ output extension=".cs" #>
    
    <#
        foreach(ModelType type in this.ModelRoot.Types)
        {
    #>
        <#= type.Name#>
    <#
        }
    #>
    

    On the Solution Explorer toolbar, click Transform All Templates.

See Also

Tasks

Walkthrough: Creating and Running Text Templates

Other Resources

Generating Specific Kinds of Artifacts from Text Templates

Adding Code to Text Templates

Generating Artifacts By Using Text Templates

Domain-Specific Language Tools Glossary

Change History

Date

History

Reason

July 2008

Rewrote and refactored project.

Content bug fix.