T4模板调试器在多个TT文件被包含时显示错误的行数

3

我正在尝试使用VS2010专业版和Windows 7调试以下T4模板文件。

但是调试器没有在文件“Texttemplate2.tt”中突出显示正确的行。

文件1:File1.tt

<#@ template debug="true" hostspecific="true" language="C#" #>

<#@ include file="Texttemplate2.tt"  #>

<#

System.Diagnostics.Debugger.Launch();


    int a= 10;

    Write("ASS");

    GetProperty("User","UserName");

#>

File : Texttemplate2.tt

<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ assembly name="System.Xml" #>
<#@ import namespace="System.Xml" #>
<#@ import namespace="System.IO" #>
<#+

 public void Load()
    {
        string doc=null;
        if(doc == null)
        {
            string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);
            string absolutePath = Path.Combine(templateDirectory,"../../App_Data/EntityUI_MetaData_Appsettings.xml");
        }
    }
 public string GetProperty(string Entity, string prop)
    {
         Load();
         string node="none";

         if (node != "0" )
         {
            if (node == Entity )
              {
                return node;
              }
         }
        return null;
    }
 #>

让我详细解释一下这个问题。我已经创建了上述两个T4模板文件。现在我想调试我的T4模板文件“File1.tt”代码(而不是生成的代码)。我所做的是使用调试器启动它。

System.Diagnostics.Debugger.Launch();

并设置断点

int a= 10;

按下F5现在会触发我的断点。
int a= 10;

现在该部分已经用黄色作为背景颜色,并配以左侧的黄色箭头来进行标注。接下来请按F11键,直到您到达目标页面。

GetProperty

当调用此方法时,您会注意到黄色箭头,这意味着当前正在执行的语句未被突出显示。

也就是说,假设第20行是下一个执行的语句,则调试器将在texttemplatefile2.tt中突出显示第10行。

1个回答

1
我遇到过同样的问题。我没有找到使行正确的方法。但是,我发现您可以添加。
System.Diagnostics.Debugger.Break();

在您想要测试并调试T4模板的行处。您的IDE仍然会停留在错误的行上,但是如果您转到代码中触发断点的位置 - 断点应该在那里 - 您至少可以检查该点的变量。
警告:如果您尝试保存或运行带有调试器行的模板,有时会导致Visual Studio崩溃。当您不使用它时,请立即将其注释掉。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接