17

I am writing software that produces C# code. Mostly I am using StringTemplate and StringBuilder.

Is there any way to use T4 templates direct from my code?

Đorđe
  • 447
  • 5
  • 18

2 Answers2

11

Oleg Sych describes how to do this here: Understanding T4: Preprocessed Text Templates. Note that it looks like you'll need Visual Studio 2010 to generate a preprocessed Text Template, but you'll be able to host the preprocessed Text Template wherever you like - including within your WinForms application.

Dan Blanchard
  • 4,014
  • 1
  • 30
  • 26
5

A simple way to do this:

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();
Diogo
  • 616
  • 6
  • 10
  • 10
    Do not assume that C:\ exists. Especially do not assume that TextTemplating is installed. – SLaks Jun 20 '11 at 21:13