I managed to solve a similar task with the following technic.
Install the T4 Toolbox extension to Visual Studio (probably not needed, I'm not sure).
Wrap your template content into a class like this:
<#+
public class MyTemplate : CSharpTemplate
{
public MyTemplate ()
{
}
public override string TransformText()
{
base.TransformText();
#>
// PUT YOUR TEMPLATE RENDERING HERE
<#+
}
}
#>
Then create another template file and call this template explicitly. There you can configure the output parameters.
<#
var mytemplate = new MyTemplate();
mytemplate.Output.Project = @"MyProject.csproj";
mytemplate.Output.File = @"MyFileRelativeToProjectFolder.cs";
mytemplate.Render();
#>
Please refer to this article for further details.