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.
Preprocessed text templates: Provide a convenient method of generating text files at run time in any application.
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.
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.
Support for Visual Studio Modelbus: Provides a standard method of accessing multiple models from a text template.
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.
Access to service provider: Available in a property of ModelingTextTransform.
Access to environment: In text template directives, you can use Visual Studio macros such as $(ProjectDir) or Windows environment variables such as %ProgramFiles%.
The following improvements have been made that might affect you if you have customized the text templating process.
Namespace and assembly changes: Some types have moved to the assembly Microsoft.VisualStudio.TextTemplating.Interfaces.
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.
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":
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:
To return to the debugger at subsequent break points, call:
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.