T4 template assembly directive
Asked Answered
T

3

10

I have a custom dll, which has a class in it, and (for simplicity's sake) there's a method on it which'll return a string.

I have a project, which references said dll, and I want to use a (not preprocessed) T4 template in that project, which calls said method. I've tried this:

<#@ template debug="true" hostspecific="false" language="C#" #>
<#@ assembly name="MyDLL.dll" #>
<#@ output extension=".cs" #>
<#
    var template = new MyDLL.MyNamespace.MyClass();
        this.Write(template.Run());
#>

I got the following errors:

Error 14 Compiling transformation: Metadata file 'MyDLL.dll' could not be found
Error 13 A namespace cannot directly contain members such as fields or methods

even if MyClass.Run() is simply a return "//hello";

Theiss answered 27/5, 2011 at 11:2 Comment(1)
T4 and how it finds assembly references depends on what version, and how you are running them. Are you using Visual Studio 2008 or 2010? Are you running them from within Visual Studio, or from TextTransform.exe?Exhilarant
G
8

Seems like Your problem:

Error Compiling transformation: Metadata file 'dotless.Core' could not be found

It's due to compatibility break described here:

http://weblogs.asp.net/lhunt/archive/2010/05/04/t4-template-error-assembly-directive-cannot-locate-referenced-assembly-in-visual-studio-2010-project.aspx

Glen answered 27/5, 2011 at 11:56 Comment(0)
D
8

I had the very same problem only yesterday, we've got a solution level Binaries folder, so the the following worked for me $(SolutionDir)Binaries\Assembly.dll.

However, depending on where the assembly is located, you may be able to use a project relative path by using the $(ProjectDir) directive...

Deni answered 27/5, 2011 at 12:7 Comment(0)
Q
0

(Note: all of this applies to VS2013. Might be different with other versions.)

First, use $(TargetDir) to find the file in your output path.

Ex: <#@ assembly name="$(TargetDir)MyDLL.dll" #>

Second, it seems that the template generator runs before references are copied to the output folder. So if you haven't successfully built at all yet, or haven't built at least once with the new reference added to the project, then the .dll will not be there.

And in fact it will never be there till you do successfully build, and if you're getting an error from the template generator that the reference can't be found, you'll never successfully build, and you're stuck.

The way to get out of this situation is to either exclude the template temporarily, get your project to build, (which will copy references), and then add it back; or else manually copy the .dlls yourself to the directory that it's complaining about. Once things are building, they should stay building.

(Since the template generator runs before references are copied, I suspect there would be a similar issue involving new code. If you add new code to a library, and make use of it in your template before building, you would get stuck with the template not being aware of the new code, which makes it throw an error, which makes your build not succeed, so it doesn't get the new version, and you're stuck again.)

(You also should end up in this situation whenever you clean or rebuild your project, but I don't seem to have that happen very often, so there may be more to this than I realize.)

Quevedo answered 15/9, 2016 at 20:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.