Setting a breakpoint in a T4 template
Set the following:
<#@ template debug="true" hostSpecific="true" #>
<#@ import namespace="System.Diagnostics" #>
Then in your template
Debugger.Launch();
VS will kick off the JIT debugger in a new instance of VS 2010
OK- figured it out. Launching a new instance is not an option, regardless of what Oleg's article says. (No diss to Mr. Sych, his blog is gospel for T4 code generation!)
- Start a second instance of Visual Studio,
- Open a file (any file) so the Debug menu shows up.
- Select "Attach to Process..." and select the other VS.NET instance
- Save your template in the attached instance of VS.NET (or right-click and select Run Custom Tool)
Voila.
In Visual Studio 2013:
- Set a breakpoint in the .tt file
- Right-click the .tt file in the solution explorer
- Select "Debug T4 Template"
- Done!
No attaching a second instance of Visual Studio needed.
Make sure that you turn on the debug option in the template directive:
<#@ template language="C#" debug="true" #>
This makes T4 save the source code and symbol files necessary for debugging in Visual Studio.