Track instance in c #?

Is there a way to track a single instance in C # /. NET in Visual Studio when debugging? I think that would be really useful sometimes.

Another way to look at this is through breakpoints on instances, not code. Therefore, every time my instance is available and / or modified, execution stops and I am given a line of code that accesses / modifies my instance.

In C ++, equivalence will control a piece of memory where an instance is located, or just a pointer to an instance. This approach does not work with managed code, since objects in .NET move around, so I need equivalence for pointers in C ++.

I am aware of WeakReferences in C #, but I'm not sure if they are used during debugging?

Edit1: This question is different from “When debugging, is there a way to find out if an object is a different instance?”, Since I am not interested in comparing two links, but I want to access one object.

+4
source share
2 answers

I don't know anything out of the box, but VS supports conditional breakpoints. One of the options:

  • Put breakpoints on all the methods of your class that interest you.
  • Debug your code until the first one is reached.
  • Find the HashCode instance
  • Compose all breakpoints on GetHashCode() == the hash code you previously retrieved
  • Let the application run until the breakpoint is deleted.
  • Look in the Call Stack window to see which line of code calls your method.

A bit awkward, but will work ...

+1
0

Source: https://habr.com/ru/post/1598305/


All Articles