The following is one way to pass parameters:
- You have to create TextTemplatingSession.
- Set the session dictionary for the parameters.
- Process the template using that session.
Sample code (Replace the ResolvePath with the location of your tt file):
<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ output extension=".txt" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
<#
string templateFile = this.Host.ResolvePath("ClassGeneration.tt");
string templateContent = File.ReadAllText(templateFile);
TextTemplatingSession session = new TextTemplatingSession();
session["namespacename"] = "MyNamespace1";
session["classname"] = "MyClassName";
var sessionHost = (ITextTemplatingSessionHost) this.Host;
sessionHost.Session = session;
Engine engine = new Engine();
string generatedContent = engine.ProcessTemplate(templateContent, this.Host);
this.Write(generatedContent); #>
I saw this example on Oleg Sych's blog, which is great resource for t4. Here is the updated link: https://web.archive.org/web/20160706191316/http://www.olegsych.com/2010/05/t4-parameter-directive