How can I trace a variable at runtime in C#?

前端 未结 5 1786
余生分开走
余生分开走 2020-12-18 05:43

How can I track a variable\'s values as they change, at runtime, in C#? I\'m interested in the same functionality that the debugger provides when I\'m tracing a variable thr

5条回答
  •  青春惊慌失措
    2020-12-18 06:36

    The only sensible way you could do that without the debugger would be: don't use a variable, but use a property, and (perhaps conditionally) add trace to the setter:

    private int myValue;
    public int MyValue {
        get {return myValue;}
        set {
            SomeTraceMethod(myValue, value, ...);
            myValue = value;
        }
    }
    

    Obviously this cannot then be used for arbitrary fields/variables.

提交回复
热议问题