views:

151

answers:

4

Right now I'm looking at a bug in some C# code where I need to get a given object instance at some location. I'm sitting on a breakpoint at that location in the debugger and can jump back up the stack and view the object instance that I need to get. I suspect that there is a way to get that instance from what I have (foo.bar.baz.bla.bla.bla or something like that) but I'm not knowledgeable enough about the code to know how to find it.

Hypothetical Example:

I'm sitting in the debugger at line 2485 in some one eases code and realize that the program needs to, right here, set the FooBat property on the enclosing WizBang object (the one that the function 27 steps up the call stack was called on) but I don't have any direct references to the enclosing WizBang object. However I suspect that one of the other object I do have access to has access to something that has access to something that does have access to the enclosing WizBang object. But that gives me about 10K things to look through and, oh by the way, I can also access 42 different WizBang objects that are not the one I want so I also need to check that it really is the same object as the one 27 steps up the stack. If I can just find how to get access to it I can add SomeExp.FooBat = true; right here on line 2485 and close this bug!

My question is: has anyone made a tool that uses reflection and bruit force to search chains of properties and members to find one that will give a desired object instance?

Yes I know this is an O(bd) problem and often won't work but it's computer time, not programmer time and when it does work, it would be fantastic!

p.s. I give it less than even odd of what I want existing (now <g/>).

A: 

Maybe you can use the watch-window of the visual-studio debugger. You could insert your instance once and watch it every step.

Emiswelt
See my comment on the question: I need an expression that I can edit into my code. This question stems from a debugging session where I realized that to fix a bug, I needed to use an object that, using the debugger, I could see in another stack frame but I couldn't figure out how to get access to from the place where I needed it.
BCS
You can modify objects on VS' Watch Window.Also, you seem to have a doubt on Reflection. You can grab pretty much everything with Reflection.
Leahn Novash
I rather suspect that poping a message box asking the end user to use the debugger to edit stuff isn't going to fly. I need an expression that can be used in the final product, not just a way to get it to run right now.
BCS
+2  A: 

maybe you should try the "Immediate window" where you can enter c# live. It can evaluate only expressions and assignments (no declarations etc).

You can find the immediate window from Debug->Windows->Immediate (Ctrl-Alt-I by default)

Did you try that already?

Johannes Rudolph
That allows me to test IF an expression gets me what I want, but it sill has me manually searching though the options.
BCS
A: 

Although reading all your comments and posts I'm not sure whether I correctly understood what you want. Did you try conditional breakpoints in Visual Studio? They may help as well. You can use them to check whether your object fulfills some conditions and just stop in that case.

Juri
A: 

I think what you're after is something that you would have to write yourself. That said, I don't imagine it to be very difficult - after all, you just need to write a method that uses reflection to traverse whatever object structure you're working with, checking for a specific condition. Then, you simply set a breakpoint (or hit Pause) and then run your method on whatever object structure you need. You can probably set the method to [Conditional(DEBUG)] so it doesn't appear in the release version of your program.

Dmitri Nesteruk
That would be a highly limited, ad-hoc solution to what I want (OTOH you seem to be the first person that gets my question <g>). I'd love to see a tool that does this, but I'm not interested enough to write one my self.
BCS