我正在编写生成 C# 代码的软件。我主要使用StringTemplate和 StringBuilder。
有什么方法可以直接从我的代码中使用 T4 模板?
我正在编写生成 C# 代码的软件。我主要使用StringTemplate和 StringBuilder。
有什么方法可以直接从我的代码中使用 T4 模板?
Oleg Sych在此处描述了如何执行此操作:了解 T4:预处理文本模板。请注意,您似乎需要 Visual Studio 2010 来生成预处理的文本模板,但您可以在任何您喜欢的地方托管预处理的文本模板 - 包括在您的 WinForms 应用程序中。
一个简单的方法来做到这一点:
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
// Set text transform program (this could change according to the Windows version)
proc.StartInfo.FileName = "C:\\Program Files (x86)\\Common Files\\microsoft shared\\TextTemplating\\10.0\\TextTransform.exe";
// Specify T4 template file
proc.StartInfo.Arguments = "C:\template.tt";
proc.Start();