How do I use custom library/project in T4 text template?
Asked Answered
R

3

7

I look and I don't see.

I have a solution with two projects -- project A (a library) and project B, which is main project and contains T4 text template.

What I did so far -- I added a reference in main project to project A. I included such line in template:

<#@ import namespace="MyProjectA" #>

Yet, there is still an error "Compiling transformation: The type or namespace name 'MyProjectA' could not be found (are you missing a using directive or an assembly reference?)"

Question: how can I reference to project A from text template?

Please note: I would like to reference a project within the solution, not the dll file on disk.

Resupinate answered 1/6, 2011 at 9:52 Comment(0)
F
4

You need to reference the DLL as well using the "assembly" directive. For instance:

<#@ assembly name=“System.Xml” #>

You can reference dlls by their path, as well. See Oleg Sych's T4 series for pretty much anything you would ever want to know. Here is the page about the "assembly" directive: https://web.archive.org/web/20160720100512/http://www.olegsych.com/2008/02/t4-assembly-directive/

I'm afraid that the T4 template is totally unaware of the solution it lives in, however, so referencing another project in the solution will still have to be done as a dll reference. If you set the HostSpecific attribute in the "template" directive like this:

<#@ template language="C#" debug="false" hostspecific="true" #>

Then you should at least be able to make the path to the other dll relative, although I haven't tried that particular trick myself. You can get the path to the current T4 file by using the Host.TemplateFile property. Try using this to construct the dll reference, such as:

<#@ assembly name=Path.GetDirectoryName(Host.TemplateFile) + “..\OtherProject\bin\Debug\ClassLibrary1.dll” #>

I can't promise that this will work, but it's worth a shot.

Freeboard answered 1/6, 2011 at 11:26 Comment(2)
Thank you, from the explanation on the blog I came to conclusion the assembly directive is simply alternative to reference (for example you could include assembly only for one template), and I thought references are seen by T4.Resupinate
´hostspecific´ and ´Path.GetDirectoryName´ can be avoided with ´$(SolutionDir)´, see link. Shame there is no cleaner solution that would autobuild referenced project on text generation if it has changed.Enright
L
13

Using $(SolutionDir) references the project via the dll in the bin folder (only way with t4 due to how it resolves assembly names)

<#@ assembly name="$(SolutionDir)MyProject\bin\Applications.Models.dll" #>
Loquitur answered 23/8, 2013 at 20:36 Comment(0)
F
4

You need to reference the DLL as well using the "assembly" directive. For instance:

<#@ assembly name=“System.Xml” #>

You can reference dlls by their path, as well. See Oleg Sych's T4 series for pretty much anything you would ever want to know. Here is the page about the "assembly" directive: https://web.archive.org/web/20160720100512/http://www.olegsych.com/2008/02/t4-assembly-directive/

I'm afraid that the T4 template is totally unaware of the solution it lives in, however, so referencing another project in the solution will still have to be done as a dll reference. If you set the HostSpecific attribute in the "template" directive like this:

<#@ template language="C#" debug="false" hostspecific="true" #>

Then you should at least be able to make the path to the other dll relative, although I haven't tried that particular trick myself. You can get the path to the current T4 file by using the Host.TemplateFile property. Try using this to construct the dll reference, such as:

<#@ assembly name=Path.GetDirectoryName(Host.TemplateFile) + “..\OtherProject\bin\Debug\ClassLibrary1.dll” #>

I can't promise that this will work, but it's worth a shot.

Freeboard answered 1/6, 2011 at 11:26 Comment(2)
Thank you, from the explanation on the blog I came to conclusion the assembly directive is simply alternative to reference (for example you could include assembly only for one template), and I thought references are seen by T4.Resupinate
´hostspecific´ and ´Path.GetDirectoryName´ can be avoided with ´$(SolutionDir)´, see link. Shame there is no cleaner solution that would autobuild referenced project on text generation if it has changed.Enright
E
0

All the methods for referencing an assembly from a T4 Template are given here: T4 Template error - Assembly Directive cannot locate referenced assembly in Visual Studio 2010 project. They still apply for later versions of VS.

If you want to use the method given by @atreeonhill and you want to use some other macro value then all the VS macros are given here: Macros for Build Commands and Properties

Endosteum answered 1/4, 2015 at 21:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.