【问题标题】:Referencing a 64 bit DLL in T4 template在 T4 模板中引用 64 位 DLL
【发布时间】:2018-05-12 21:52:52
【问题描述】:

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

【问题讨论】:

    标签: visual-studio 64-bit t4


    【解决方案1】:

    看起来 TextTemplatingFileGenerator 工具运行在 32 位进程中,无法将 64 位程序集导入 T4 模板。

    我找到了两种选择

    1. 编写一个从 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 文件。我没有在这里分享代码,但我检查了它是否工作正常。

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


    【讨论】:

      猜你喜欢
      • 2013-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多