如果我在像这样的 T4 模板中引用 64 位 DLL
<#@ assembly name="path\to\64bit.dll" #>
并用 TextTransform.exe 运行它,它会给出一个异常,说
There was a problem loading the assembly 'path\to\64bit.dll' The following Exception was thrown:
System.BadImageFormatException: Could not load file or assembly 'file:///path\to64bit.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format.
是否有 64 位版本的 TextTransform.exe,或者是否有其他方法可以实现此功能?
最佳答案
看起来 TextTemplatingFileGenerator 工具在 32 位进程中运行,不可能将 64 位程序集导入 T4 模板。
我找到了两个选择
编写自定义模板类,它继承自 Microsoft.VisualStudio.TextTemplating 程序集中的 TextTemplating 类。我还认为,如果存在通过复杂代码生成的文本,这比使用 tt 文件更好,因为它更易于理解和管理。这是一个例子。
在 64 位程序集中namespace SixtyFourBitClassLibrary { public class Class1 { public string SampleString = "Sample"; } }
在主程序集中(也是 64 位)
namespace ExecuteSixtyFourBitAssemblyInTT
{
class Program
{
static void Main(string[] args)
{
SampleTemplate st = new SampleTemplate();
var s = st.TransformText();
Console.WriteLine(s);
}
}
class SampleTemplate : TextTransformation
{
public override string TransformText()
{
Class1 cls = new Class1();
return string.Format("{0} OK",cls.SampleString);
}
}
}
2. 创建自定义文本模板主机。你可以查看这个Walkthrough: Creating a Custom Text Template Host .您可以通过此方法使用现有的 tt 文件。我没有在这里分享代码,但我检查过它是否有效。
这两种解决方案都需要另一种方式(可能是扩展)来开始生成。
关于visual-studio - 在 T4 模板中引用 64 位 DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47548542/