3

My item.tt file references a custom dll. This is being done with the assembly tag which contains an absolute path to the dll and then and import for the associated namespace. I want a way for that absolute path to be relative however when I've changed this to not be an absolute path I get an error which states that the file could not be found in C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\. Is there a way to make this a relative path or am I stuck between either using an absolute path or require placing the dll in that path on every dev's environment?

1

The Assembly Directive requires an absolute path. But you can use variables to make the path "relative" from a known path - for example in the Fortis T4 template we add this:

<#@ Assembly Name="$(SolutionDir)\packages\Fortis.3.5.5.2\lib\net45\Fortis.dll" #>
<#@ assembly name="$(ProjectDir)$(OutDir)Transitus.Rainbow.dll" #>

Than we can have a binary that is in the repo, or just built into the output project or just something from a NuGet package referenced in the T4 template.

You can also use environment variables by using %variableName% syntax. the $(variableName) syntax is used for Visual Studio variables.

Reference: Using the Assembly Directive

Alternate Solution

If that is not working for you, you could try the AssemblyReferences.tt project by @herskinduk https://github.com/herskinduk/AssemblyReferencesTT

This project adds a new tt file called AssemblyReferences.tt that can be used to build T4 references based on the projects references. Any references with a hint path and solution references will be listed in that file. Then you can include that file in your main T4 template like this:

<#@ include file="AssemblyReferences.tt" #>

The binary you are trying to reference must be included in your project with a hint path or as a solution reference.

Install it as a NuGet package: https://www.nuget.org/packages/AssemblyReferencesTT/

4
  • That makes sense but when using regenerate code for all items I get the following error showing the token is not replaced: Could not find file 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\$(SolutionDir)MyCustomCode.dll'. I'm not sure if this is a tds limitation or not as I've seen other t4 file questions having answers referencing similar solutions (stackoverflow.com/a/25079201/2597149). My assembly node in the tt file is <#@ assembly name="$(SolutionDir)MyCustomCode.dll" #> – Teeknow Jun 7 '17 at 15:35
  • you are missing a backslash between $(SolutionDir) and MyCustomCode.dll - also updated the answer with a 2nd option. – Richard Seal Jun 7 '17 at 15:43
  • Unfortunately the slash was just tacked on to the error message: Could not find a part of the path 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\$(SolutionDir)\MyCustomCode.dll'. I will look into the alternate solution when I have time. – Teeknow Jun 7 '17 at 20:06
  • For now the project is going to copy the dll to the public assemblies folder so the assembly reference doesn't need an absolute path since those events can parse visual studio tokens. – Teeknow Jun 7 '17 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.