简体   繁体   中英

Can I use T4 programmatically from C#?

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM