'In visual studio, how do you break the debugger when a property changes that you do not own in a managed language (like C#)
I've got a weird situation where something is setting the TopMost property of the main MDI form to true. After much trial and error we're pretty sure this must be coming from a third party library but it's not in any places that we would expect.
It would be VERY easy to fix this if we could just set the application to break in the debugger whenever this property was set. However this property is defined by the WinForms libraries so we can't just put a breakpoint in there.
For cases where you're working with a library that you don't have the source code for this would be invaluable but I fear there is no solution to this problem since Data Breakpoints are not supported for managed languages (as far as I know).
So, in C# is there a way that you know of to break when a property gets changed when you don't have access to place a breakpoint in the setter? This could easily solve many edge case issues where things get changed for reasons you can't anticipate.
Solution 1:[1]
It is possible to set breakpoints on code you don't own. Since TopMost
is a property, all you have to do is putting a breakpoint on the setter.
Just open the "breakpoints" panel in Visual Studio (Debug -> Windows -> Breakpoints), click on "New -> Function Breakpoint" then type:
System.Windows.Forms.Form.set_TopMost
Run your application (make sure the symbol are loaded), and profit.
Note that you also need to make sure that "Just my code" is disabled. Go in Tools -> Options -> Debugging -> General, and uncheck "Enable Just My Code".
Solution 2:[2]
Go to
Tools
>Options
>Debugging
>General
and uncheckEnable Just My Code
checkbox.Identify which function you need to set breakpoint on. It's not always that obvious - one thing you can do is to write set given property value in code, start debugging, then right click and select
Go To Disassembly
, you will see listing like this:
So in this case I wanted to set breakpoint on PresentationTraceSources.DataBindingSource.Switch.Level
-
property change, but I needed to set breakpoint on
System.Diagnostics.SourceSwitch.set_Level
.
Debug
>New Breakpoint
>Function Breakpoint
.
Type function name where to set breakpoint. In my case it was System.Diagnostics.SourceSwitch.set_Level
.
Please notice that you will need to set same breakpoint for each new session.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Kevin Gosse |
Solution 2 | TarmoPikaro |