visual-studio 无法在 T4 模板中引用程序集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3434713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 12:19:46  来源:igfitidea点击:

Can't reference an assembly in a T4 template

visual-studiovisual-studio-2010t4

提问by ProfK

I have the following code in a tester class in my main assembly, PocoGenerator. This assembly is supposed to use a T4 template to generate POCO's based on L2S entities in a referenced assembly (a project reference), DataObjects.

我在主程序集的测试器类中有以下代码,PocoGenerator. 该程序集应该使用 T4 模板基于引用程序集(项目引用)中的 L2S 实体生成 POCO DataObjects

var assemblyName = "DataObjects";
var dataObjects = AppDomain.CurrentDomain.Load(new AssemblyName(assemblyName));

Try as I may, I cannot get T4 to find the DataObjects assembly. I have tried various forms of assemblydirectives, like:

尽我所能,我无法让 T4 找到 DataObjects 程序集。我尝试了各种形式的assembly指令,例如:

<#@ assembly name="DataObjects" #>
<#@ assembly name="DataObjects, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" #>

to no avail. The code above works in the tester class, but not in the template. What am I doing wrong?

无济于事。上面的代码在 tester 类中有效,但在模板中无效。我究竟做错了什么?

ADDED: I have resolved this issue by using the absolute path to the assembly in bot places I reference it, the directive as well as the class feature block, i.e.

添加:我已经通过在我引用的机器人位置、指令以及类功能块中使用程序集的绝对路径解决了这个问题,即

<#@ assembly name="C:\Development\PocoGenerator\DataObjects\bin\Debug\DataObjects.dll" #>

and

var sourceAssembly = Assembly.LoadFile(@"C:\Development\PocoGenerator\DataObjects\bin\Debug\DataObjects.dll");

But I really don't like this, as I would like to use this template in various projects, and I just plain hate duplication, especially of magic strings.

但我真的不喜欢这个,因为我想在各种项目中使用这个模板,我只是讨厌重复,尤其是魔术字符串。

回答by Eric

<#@ assembly name="$(ProjectDir)bin\Debug\ProofOfConcept.dll" #>

Happy Coding!

快乐编码!

回答by Peter Stegnar

To reference assembly in T4 template in VS2010 you have some options:

要在 VS2010 中引用 T4 模板中的程序集,您有一些选择:

  1. GAC your assemblies and use Namespace Reference or Fully Qualified Type Name
  2. Use a hard-coded Fully Qualified UNC path
  3. Copy assembly to Visual Studio "Public Assemblies Folder" and use Namespace Reference or Fully Qualified Type Name.
  4. Use or Define a Windows Environment Variable to build a Fully Qualified UNC path.
  5. Use a Visual Studio Macro to build a Fully Qualified UNC path.
  1. GAC 您的程序集并使用命名空间引用或完全限定类型名称
  2. 使用硬编码的完全限定 UNC 路径
  3. 将程序集复制到 Visual Studio“公共程序集文件夹”并使用命名空间引用或完全限定类型名称。
  4. 使用或定义 Windows 环境变量来构建完全限定的 UNC 路径。
  5. 使用 Visual Studio 宏构建完全限定的 UNC 路径。

I would suggest that you put a referenced assembly in your Public Assemblies Folder, another, maybe even better solution would be to hard code the path of your referenced assemblies.

我建议您将引用的程序集放在公共程序集文件夹中,另一个甚至更好的解决方案是对引用的程序集的路径进行硬编码。

Very nice post on this topic: T4 Template error - Assembly Directive cannot locate referenced assembly in Visual Studio 2010 project.

关于此主题的非常好的帖子:T4 模板错误 - 程序集指令无法在 Visual Studio 2010 项目中找到引用的程序集。

Basically MS decided to the the braking change, that the project referenced assemblies are not referenced by T4 engine, too.

基本上 MS 决定改变刹车,项目引用的组件也没有被 T4 引擎引用。

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.

T4 的程序集集与包含项目的程序集集完全分离,以避免在项目面向以前的框架版本时选择错误的程序集。项目程序集不再用于解析模板程序集指令。

More on that: What's new in T4 in Visual Studio 2010

更多相关信息:Visual Studio 2010 中 T4 的新增功能

回答by Regent

I had a similar problem when I tried to include Less Css for .NETin my Web project.

当我尝试在我的 Web 项目中包含Less Css for .NET时,我遇到了类似的问题。

I've ended up with copying the assembly in the root folder of my project and including it as a reference in the project itself. Then, I've added the following lines in the .tt file:

我最终将程序集复制到了项目的根文件夹中,并将其作为参考包含在项目本身中。然后,我在 .tt 文件中添加了以下几行:

<#@ assembly name="dotless.Core.dll" #>

<#@ import namespace="dotless.Core" #>
<#@ import namespace="dotless.Core.configuration" #>

I'm sure that something similar should work with your assembly as well...

我相信类似的东西也应该适用于你的程序集......

回答by Mark Mullin

I've found there are a number of cases in creating and using the gax toolkit and packages where the build is perfectly happy with the way references are structured but the runtime gets all bothered because it can't find what it's looking for - this usually occurs when the main assembly references an assembly that uses gax elements and then that assembly in turn references another assembly that the main does not.

我发现在创建和使用 gax 工具包和包时有很多情况,其中构建对引用的结构方式非常满意,但运行时却因为找不到它要找的东西而烦恼——这通常是当主程序集引用使用 gax 元素的程序集,然后该程序集又引用另一个主程序集不使用的程序集时,会发生这种情况。

try directly including the assembly in question in your main assembly - and consider that you may need to write post build instructions to move it to an 'expected' location - while a nusiance, it should beat having to hardwire the path.

尝试将有问题的程序集直接包含在您的主程序集中 - 并考虑您可能需要编写构建后指令以将其移动到“预期”位置 - 虽然麻烦,但它应该击败必须硬连线路径。

YMMV

青年会