T4 Assembly Directive

Updated: March 2011

In a Visual Studio design-time text template, the assembly directive loads an assembly so that your template code can use its types. The effect is similar to adding an assembly reference in a Visual Studio project.

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


You do not need the assembly directive in a run-time (preprocessed) text template. Instead, add the necessary assemblies to the References of your Visual Studio project.

The syntax of the directive is as follows:

<#@ assembly name="[assembly strong name|assembly file name]" #>

The assembly name should be one of the following:

  • The strong name of an assembly in the GAC, such as System.Xml.dll. You can also use the long form, such as name="System.Xml, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089". For more information, see AssemblyName.

  • The absolute path of the assembly

You can use the $(variableName) syntax to reference Visual Studio or MSBuild variables such as $(SolutionDir), and %VariableName% to reference environment variables. For example:

<#@ assembly name="$(SolutionDir)\MyProject\bin\Debug\SomeLibrary.Dll" #>

The assembly directive has no effect in a preprocessed text template. Instead, include the necessary references in the References section of your Visual Studio project. For more information, see Run-Time Text Generation by using Preprocessed T4 Text Templates.

The following assemblies are loaded automatically, so that you do not need to write assembly directives for them:

  • Microsoft.VisualStudio.TextTemplating.1*.dll

  • System.dll

  • WindowsBase.dll

If you use a custom directive, the directive processor might load additional assemblies. For example, if you write templates for a domain-specific language (DSL), you do not need to write assembly directives for the following assemblies:

  • Microsoft.VisualStudio.Modeling.Sdk.1*.dll

  • Microsoft.VisualStudio.Modeling.Sdk.Diagrams.1*.dsl

  • Microsoft.VisualStudio.TextTemplating.Modeling.1*.dll

  • The assembly containing your DSL.




March 2011

Split from parent topic.

Information enhancement.

Community Additions