开发者

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

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 "Compil开发者_C百科ing 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.


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" #>


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: 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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜