15

It's been a while since I last used T4 and this is probably a silly question...

Is it possible to reference an arbitrary assembly from a template?

Example:

  • I have a class that I'd like to use in Project X
  • Project X.Test references X and contains the .tt file

I assume the following should work

<#@ assembly name="X" #>

But I get the following error on save:

Compiling transformation: Metadata file 'X' could not be found

What am I doing wrong?

(In case anyone's interested: I'm trying to automatically generate a particular type of tests based on some metadata that I get from X)

Update: it looks like VS2010 has broken the assembly resolution behavior that I was expecting. From Link:

T4's assembly set is completely separated from the containing project's assembly set to avoid picking up the wrong assemblies when a project targets previous framework versions. Project assemblies are no longer used to resolve template assembly directives.

Are there any workarounds, besides using absolute paths?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
  • Have you tried using the fully qualified path for that assembly? The text templating tool is an external tool and probably doesn't know about projects you have loaded into VS. – Mark H Jul 13 '10 at 21:54
  • That works, but I need this to run in multiple machines that might have the project in a different path. – Diego Mijelshon Jul 13 '10 at 21:58

3 Answers3

25

You can use VS macro variables such as $(SolutionDir) in your reference as of VS2010 e.g.

<#@ assembly name="$(SolutionDir)\Project1\bin\debug\Foo.dll" #>
abatishchev
  • 98,240
  • 88
  • 296
  • 433
GarethJ
  • 6,496
  • 32
  • 42
  • This isn't working for me. Any ideas on what I'm doing wrong? My assembly ref is as follows: <#@ assembly name="$(SolutionDir)\DataObjects\bin\Debug\DataObjects.dll" #> – ProfK Aug 08 '10 at 14:49
  • That shoudl work just fine, but it sounds from the otehr threads liek you are using VS 2008, not VS2010? If you're usign VS2008, the recommended approach is to add the assembly as a project reference in the hosting project instead. – GarethJ Aug 09 '10 at 21:13
  • 3
    +1 very useful. I had to reference the current assembly and used the following <#@ assembly name="$(TargetPath)"#> – JanW Apr 02 '12 at 10:06
  • How about when editing the scaffolding tts in Mvc? MVCWebApplication1\CodeTemplates\AddView\CSHTML\Edit.tt. Get folowing to work in a simple .tt but not in Edit.tt: <#@ assembly name="$(TargetDir)\ToSpinitLibraryModel.dll"#> Also tried via "$(SolutionDir) – Per G Jun 06 '13 at 13:59
  • No, I'm afraid not, as that uses a different host at present. You could log a connect bug or post on asp.net uservoice to request this. – GarethJ Jun 08 '13 at 09:55
1

You can also check here on SO: Can't reference an assembly in a T4 template

Community
  • 1
  • 1
Peter Stegnar
  • 12,615
  • 12
  • 62
  • 80
0

@GarethJ gives a good answer but for all the methods of referencing an assembly from a T4 template try this: T4 Template error - Assembly Directive cannot locate referenced assembly in Visual Studio 2010 project.

And if you like the VS Macro solution then you can find 'em all here: Macros for Build Commands and Properties

grahamesd
  • 4,773
  • 1
  • 27
  • 27