Udostępnij za pośrednictwem


What's New in Code Generation and Text Templates

Visual Studio text templates allow you to generate code and resources for your application, and also any kind of text file or document at run time.

What’s New in Service Pack 1 (SP1)

You can obtain the following features by installing SP1 for Visual Studio 2010.

  • Debugging assemblies
    If your text template loads an assembly that you are debugging, you can rebuild the assembly without difficulty.

    In versions of Visual Studio prior to SP1, it is sometimes difficult to rebuild an assembly that has been recently loaded by a template, because the template locks the assembly. From SP1, the template loads a copy of the assembly, so that the original assembly is not locked. If you rebuild the assembly, then on the next occasion when the template is executed, it will reload a new copy of the assembly. For more information, see T4 Assembly Directive.

  • Inheritance between Text Templates
    You can define a text template that contains a framework of text blocks that is common to several variants. The variant templates can inherit from the common template. For examples and more information, see T4 Template Directive and Run-Time Text Generation by using Preprocessed T4 Text Templates

New and Enhanced Features in Visual Studio 2010

Feature

Topic

Preprocessed text templates: Provide a convenient method of generating text files at run time in any application.

Run-Time Text Generation by using Preprocessed T4 Text Templates

Build integration: Makes sure that your generated program files are kept up to date with their source files or model. This feature is available when you install Domain-Specific Language Tools.

Code Generation in a Build Process

Text transformation tasks for MsBuild: Allow you to transform specific files in a project when building the project in Visual Studio the command line.

If you have scripts that call TextTransform.exe, you might find this method more flexible.

Code Generation in a Build Process

Support for Visual Studio Modelbus: Provides a standard method of accessing multiple models from a text template.

Microsoft.VisualStudio.TextTemplating.Modeling.ModelBusEnabledTextTransformation

Separation from DSLs and models: You do not need a domain specific language to use text templates, even though the two features were originally introduced at the same time. Text templates and file generation are a built-in part of Visual Studio.

Generating Code from a Domain-Specific Language

Access to service provider: Available in a property of ModelingTextTransform.

T4 Text Template Utility Methods

Access to environment: In text template directives, you can use Visual Studio macros such as $(ProjectDir) or Windows environment variables such as %ProgramFiles%.

T4 Text Template Directives

Changes in Customization

The following improvements have been made that might affect you if you have customized the text templating process.

Feature

Topic

Namespace and assembly changes: Some types have moved to the assembly Microsoft.VisualStudio.TextTemplating.Interfaces.

Walkthrough: Creating a Custom Directive Processor

Walkthrough: Creating a Custom Text Template Host

Session dictionary: A serialized dictionary that is passed into the application domain where a text template is executed. It provides a standard way for a text templating host to pass data into the text template code.

Walkthrough: Creating a Custom Text Template Host

Other Changes since Visual Studio 2008

Assemblies Referenced in Text Templates

The current project's references are no longer used to find assemblies.

In an assembly directive, state the location of the required assembly explicitly.

You can use Visual Studio macros such as $(ProjectDir) or Windows environment variables such as %ProgramFiles% as part of the assembly location.

Text Transform Assemblies

The assemblies that you might need to reference to use text templates all have the suffix "10.0":

  • Microsoft.VisualStudio.TextTemplating.10.0.dll

  • Microsoft.VisualStudio.TextTemplating.Interfaces.10.0.dll

  • Microsoft.VisualStudio.TextTemplating.Vshost.10.0.dll

  • Microsoft.VisualStudio.TextTemplating.Modeling.10.0.dll

Namespaces

There is no namespace "Microsoft.VisualStudio.TextTemplating.Interfaces". This is a change from the Beta2 release of Visual Studio 2010.

Launching the Debugger

To launch the debugger from a text template, insert this call at the point where you want to start stepping through execution:

System.Diagnostics.Debugger.Launch();

To return to the debugger at subsequent break points, call:

System.Diagnostics.Debugger.Break();

Default Language Version

The latest version (4) of the compiler will always be used to compile text templates. In Visual Studio 2008, the default compiler is 2.0, but you can specify a later version of the template programming language - for example <#@ template language="VBv3.5" #>. In Visual Studio 2010, the version number will be ignored and a warning is reported. If you have a template that you want to use in both versions of Visual Studio, you can ignore the warning.

See Also

Concepts

What's New in Visual Studio 2010