Suppose I want an object with a list of strings and every method that this object gets passed to adds its name to the list of strings. Basically it’s a stack trace, except unlike a stack trace it keeps a record of everywhere it’s previously been.
In order to do this, I would have to put code in every method it goes through to add itself but if it went through methods that didn’t know to add themselves then of course it wouldn’t have a record of having been through those methods.
It would be cool if it were possible to make an object self aware, so to speak, of the methods it goes through but I don’t think it is. But I’m wondering if there is any other way to get a record of all methods the object goes through, including those methods that don’t know to update the object?
EDIT: Currently, I’m thinking the closest you could come to solving this would be to make any property/method that’s accessed on the object update itself at that point in time. of course this wouldn’t catch places that it simply passes through without being accessed.
15
I would investigate non-coding solutions first:
-
ReSharper – Find Usages
-
Visual Studio Ultimate – Map Dependencies
6
Take a look at the StackTrace
and StackFrame
objects in the System.Diagnostics
namespace. They may be able to help log out information that is desired. When an exception occurs the stack trace in the exception is captured in a similar manner. It sounds like you want to log non-exception situations as well, so this may help as one can get the stack and frame at any point in the code.