我正在尝试使用VS2010 Professional Windows 7调试以下T4模板文件。
但是调试器不会在File:“ 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");
#>
文件: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;
现在使用黄色突出显示为BACKGOUND,使用黄色箭头突出显示为LEFT。之后按F11,直到到达
GetProperty
当在其中调用此方法时,您会注意到YELLOW ARROW,这意味着当前执行的语句未突出显示。
意思是,假设Line:20是下一条执行语句,调试器将在texttemplatefile2.tt中突出显示line:10
最佳答案
我遇到了同样的问题。我还没有找到正确的方法。但是,我发现您可以添加
System.Diagnostics.Debugger.Break();
在您要测试的行上,然后调试T4模板。您的IDE仍将处于错误的行上,但是如果您转到触发中断的代码所在的位置-中断应该在的位置,则至少可以在该点检查变量。
警告:如果尝试使用调试器行保存或运行模板,则有时会使Visual Studio崩溃。不使用时立即将其注释掉。
关于c# - T4模板调试器显示错误的行,同时包含多个TT文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7873262/