visual-studio - 在 T4 模板中引用 64 位 DLL

标签 visual-studio 64-bit t4

如果我在像这样的 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 模板。

我找到了两个选择

  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 文件。我没有在这里分享代码,但我检查过它是否有效。

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


关于visual-studio - 在 T4 模板中引用 64 位 DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47548542/

相关文章:

visual-studio - 使用 chrome 和 visual studio.net 调试 asp.net - 与 IE 相比非常慢

ios - 是否可以从 Visual Studio 2013 为 iOS 构建静态(.a)库?

java - 如何为 Hadoop 2 构建 native 库

delphi - 在64位系统中如何从PID获取系统的进程路径?

c# - T4 工具箱 Linq2Sql

c# - 本地窗口为空(Visual Studio 2015)

visual-studio - Visual Studio Test Runner 是否支持其他框架?

ios - 为 64 位架构更新 Phonegap iOS 应用程序的问题

c# - 包含在 T4 模板中只能使用一次

c# - T4MVC 正在生成 T4MVC.cs 和 T4MVC1.cs