Error when using a conditional breakpoint on System.Type

删除回忆录丶 提交于 2019-11-27 13:27:46

In my case I was using Visual Studio 2013, NUnit 2.6.4, and attaching a debugger to a unit test session, and I was getting a similar message:

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.

This was caused by a missing feature in the new debug engine Microsoft had introduced, apparently. Following instructions from this msdn blogpost I got things to work. The instructions boil down to:

  1. From the "Tools" menu open "Options"
  2. On the left hand side pick "Debugging", "General"
  3. Scroll all the way down to check "Use Managed Compatibility Mode"

This should switch to the legacy debug engine, which in my case allowed for expressions on Type in break point conditions. Note that you do need to restart your app or debugging session, obviously.

Disclaimer: I have no idea what other effects checking this option had. Personally, I turned it back off when I was done with the task that required it...

You say that Type.FullName == "Malt.Organisation" causes it to break, have you tried this.Type.FullName == "Malt.Organisation"?

Another possibility, does the debugger think you are trying to invoke a static method with having the variable named Type like its class name? Does renaming the Type variable to something else fix it?

I ran into this but when testing for IsInterface in a Web Application. Instead of enabling extra features in the debugger, I simply cheated.

bool blnIsInterface = tType.IsInterface;

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

So in your case your could do something like

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

It's a bit cumbersome but at least you won't have to worry about performance hits and enabling Native code debugging doesn't seem to be an option in Web Applications.

I'm not sure about "Use Managed Compatibility Mode" solution described here - did not help me, but in my own case Project > Properties > Debug > Enable Native code debugging - must be unchecked.

Why - no clue currently.

Was using .net 4.5, vs2015, console application.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!