Apr 1, 2010

Debugging T4

1. add "template debug="true""
2. cd %temp%, and find the latest generated cs file
3. Add System.Diagnostics.Debugger.Break() in you template file, below is example of simple template.
namespace Microsoft.VisualStudio.TextTemplating28A7CF507D33073FC06B43678F225DE4
{
    using System;
    
    
    #line 1 "C:\other_projects\Vs.netCustomization\T4Demo\Template1.t4"
    public class GeneratedTextTransformation : Microsoft.VisualStudio.TextTemplating.TextTransformation
    {
        public override string TransformText()
        {
            try
            {
                
                #line 3 "C:\other_projects\Vs.netCustomization\T4Demo\Template1.t4"
 
System.Diagnostics.Debugger.Break();

this.WriteLine("hello");


                
                #line default
                #line hidden
            }
            catch (System.Exception e)
            {
                e.Data["TextTemplatingProgress"] = this.GenerationEnvironment.ToString();
                throw;
            }
            return this.GenerationEnvironment.ToString();
        }
    }
    
    #line default
    #line hidden
}

The based class's dll in in C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Microsoft.VisualStudio.TextTemplating.10.0\v4.0_10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.TextTemplating.10.0.dll, Microsoft.VisualStudio.TextTemplating.10.0, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a he text template transformation process has two steps. In the first step, the text template transformation engine creates a class that is referred to as the generated transformation class. In the second step, the engine compiles and executes the generated transformation class, to produce the generated text output. The generated transformation class inherits from TextTransformation. Any class specified in an inherits directive in a text template must itself inherit from TextTransformation. TransformText is the only abstract member of this class.