R.D
R.D

Reputation: 4871

Can T4 templates use the same library

I have a library that defines a class

namespace ClassLibrary1
{
    public class Class1
    {
        public static readonly int Rate = 5;
    }
}

In the same lib I add a tt file

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".cs" #>
<#@ import namespace="ClassLibrary1" #>
<#@ assembly name="ClassLibrary.dll" #>

enum  MilkRate{ Walmart=<#= Class1.Rate #>}

I get error "Compiling transformation: Metadata file 'ClassLibrary.dll' could not be found d:\documents\visual studio 10\Projects\ConsoleApplication2\ClassLibrary1\TextTemplate1.tt"

how to solve this?

[Occurs on VS 2010, VS2008]

Upvotes: 1

Views: 579

Answers (3)

GarethJ
GarethJ

Reputation: 6606

You can do this but there are issues with this 'recursive' style of working that require care.

In VS2010, you can use VS Macro variables, $(SolutionDir), $(ProjectDir) and friends in your <#@ assembly #> directive, e.g.

<#@ assembly name="$(ProjectDir)$(OutDir)ClassLibrary.dll" #>

If you then have a developer do a clean get from source control, they'll have to do a build before they can do a working transform.

If they transform first, the transform will fail, then their build will fail and they'll be stuck and have to revert files. It's no big deal, but it can trip folsk up.

Upvotes: 1

Esther Fan - MSFT
Esther Fan - MSFT

Reputation: 8546

Have you tried posting to the VS Extensibility forum at http://social.msdn.microsoft.com/Forums/en-US/vsx/threads?

Upvotes: 0

Wilfred Knievel
Wilfred Knievel

Reputation: 3245

I'm not sure if that is possible.

I think the problem I might have with that is: your template is generating code that will change the dll that it is referencing!

One thing you can do is have one template reference another template using:

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

In that way you could define Rate = 5 in one central template and have everything else reference that. You could even build a template to generate your Class1 class so everything is held in one central place.

I know that doesn't directly answer your question but I hope it's useful any way.

Upvotes: 1

Related Questions