【问题标题】:Can I use T4 programmatically from C#?我可以从 C# 以编程方式使用 T4 吗?
【发布时间】:2009-10-23 11:34:08
【问题描述】:

我正在编写生成 C# 代码的软件。我主要使用StringTemplate 和StringBuilder。

有什么方法可以直接从我的代码中使用 T4 模板?

【问题讨论】:

  • 您是否尝试从 C# 自动化 T4
  • 没错。我有 WinForms 应用程序,用户在其中创建服务,然后应用程序需要生成 c# (c,java...) 代码。

标签: c# t4 template-engine


【解决方案1】:

Oleg Sych 在此处描述如何执行此操作:Understanding T4: Preprocessed Text Templates。请注意,您似乎需要 Visual Studio 2010 来生成预处理的文本模板,但您可以在任何您喜欢的地方托管预处理的文本模板 - 包括在您的 WinForms 应用程序中。

【讨论】:

  • 第二个链接现在失效了。
  • 指向archive.org上相应页面的第二个链接。 HTH
【解决方案2】:

一个简单的方法:

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();

【讨论】:

  • 不要假设 C:\ 存在。尤其不要假设已安装 TextTemplating。
猜你喜欢
  • 1970-01-01
  • 2015-08-17
  • 2015-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-17
  • 2011-04-12
相关资源
最近更新 更多