Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 836
Mohgeroth Avatar asked Feb 04 '23 12:02

Mohgeroth


1 Answers

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".

like image 112
Kevin Gosse Avatar answered Feb 14 '23 01:02

Kevin Gosse