在T4模板中引用64位DLL

3
如果我在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?还是有其他方法可以使其工作?

1个回答

0

看起来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文件来实现此方法。我不在这里分享代码,但我已经检查过它是有效的。

这两种解决方案都需要另一种方式(可能是vs扩展)来启动生成。



网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接