T4 Assembly 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 Assembly Directive.
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 |
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 asname="System.Xml, Version=4.0.0.0, 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 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 with 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*.dllSystem.dllWindowsBase.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*.dllMicrosoft.VisualStudio.Modeling.Sdk.Diagrams.1*.dslMicrosoft.VisualStudio.TextTemplating.Modeling.1*.dllThe assembly containing your DSL.
Visual Studio macros like $(SolutionDir) don’t work in MSBuild. If you want to transform templates in your build machine, you have to use project properties instead.
Edit your .csproj or .vbproj file to define a project property. This example defines a property named myLibFolder:
<!-- Define a project property, myLibFolder: --> <PropertyGroup> <myLibFolder>$(MSBuildProjectDirectory)\..\libs</myLibFolder> </PropertyGroup> <!-- Tell the MSBuild T4 task to make the property available: --> <ItemGroup> <T4ParameterValues Include="myLibFolder"> <Value>$(myLibFolder)</Value> </T4ParameterValues> </ItemGroup>
Now you can use your project property in text templates, which transform correctly in both Visual Studio and MSBuild:
<#@ assembly name="$(myLibFolder)\MyLib.dll" #>