T4 Import Directive

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at T4 Import Directive.

In the code blocks of a Visual Studio T4 text template, the import directive allows you to refer to elements in another namespace without providing a fully-qualified name. It is the equivalent of using in C# or imports in Visual Basic.

For a general overview of writing T4 text templates, see Writing a T4 Text Template.

<#@ import namespace="namespace" #>  

In this example, template code can omit an explicit namespace for members of System.IO:

<#@ import namespace="System.IO" #>  
<#   
   string fileContent = File.ReadAllText("C:\x.txt"); // System.IO.File  
#>   
The file contains: <#=  fileContent #>  

The following namespace is imported automatically, so that you do not need to write an import directive for it:

  • System

In addition, if you use a custom directive, the directive processor might import some namespaces automatically.

For example, if you write templates for a domain-specific language (DSL), you do not need to write import directives for the following namespaces:

  • Microsoft.VisualStudio.Modeling

  • Your DSL’s namespace

T4 Assembly Directive

Show: