在 System.Type 上使用条件断点时出错

Error when using a conditional breakpoint on System.Type

这是函数:

public void Init(System.Type Type) {
    this.Type = Type;
    BuildFieldAttributes();
    BuildDataColumns(FieldAttributes);
}

我在第一行 (this.Type = Type) 设置了一个断点,我想在 Type.FullName == "Malt.Organisation" 时中断,所以这就是我输入的条件。

但是在命中该行时显示如下错误:

The condition for a breakpoint failed to execute. The condition was 'Type.FullName == "Malt.Organisation"'. The error returned was 'Inspecting the state of an object in the debuggee of type System.Type is not supported in this context.'

我做错了什么(明显的)事情?

PS。解决方法是将其添加到代码中:

if (Type.FullName == "Malt.Organisation") System.Diagnostics.Debugger.Break();

你说Type.FullName == "Malt.Organisation"导致它坏了,你试过this.Type.FullName == "Malt.Organisation"吗?

另一种可能性,调试器是否认为您正在尝试调用静态方法,并使名为 Type 的变量与其 class 名称相似?将 Type 变量重命名为其他名称是否可以修复它?

⚠ 请注意,自 Visual Studio 2022 年起似乎不再可用。


在我的例子中,我使用的是 Visual Studio 2013,NUnit 2.6.4,并将调试器附加到单元测试会话,我收到了类似的消息:

The condition for a breakpoint failed to execute. The condition was 'type.Name.Contains("FooBar")'. The error returned was 'Inspecting the state of an object in the debuggee of type System.Type is not supported in this context.'. Click OK to stop at this breakpoint.

这显然是由于 Microsoft 引入的新调试引擎中缺少一个功能造成的。按照 this msdn blogpost 的指示,我开始工作了。说明归结为:

  1. 从“工具”菜单打开“选项
  2. 在左侧选择“调试”、“常规
  3. 一直向下滚动以选中“使用托管兼容模式

这应该切换到旧版调试引擎,在我的例子中,它允许在断点条件下使用 Type 表达式。请注意,您确实需要重新启动您的应用程序或调试会话,这很明显。

免责声明:我不知道检查这个选项还有什么其他影响。就个人而言,当我完成需要它的任务时,我将其关闭...

我不确定 "Use Managed Compatibility Mode" 此处描述的解决方案 - 对我没有帮助,但在我自己的情况下 Project > Properties > Debug > Enable Native code debugging - 必须取消选中。

为什么 - 目前没有任何线索。

正在使用 .net 4.5、vs2015、控制台应用程序。

我 运行 进入这个但是在 Web 应用程序中测试 IsInterface 时。我没有在调试器中启用额外的功能,而是作弊。

bool blnIsInterface = tType.IsInterface;

//Insert breakpoint here...
if(blnIsInterface)
{
    ...
}

所以在你的情况下你可以做类似的事情

public void Init(System.Type Type) {
    bool blnBreak = Type.FullName == "Malt.Organisation";
    //insert breakpoint of blnBreak == true
    this.Type = Type;
    BuildFieldAttributes();
    BuildDataColumns(FieldAttributes);
}

这有点麻烦,但至少您不必担心性能下降,而且启用本机代码调试似乎不是 Web 应用程序中的一个选项。