问题
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?
回答1:
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.
回答2:
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();
来源:https://stackoverflow.com/questions/1612846/can-i-use-t4-programmatically-from-c