I'm looking for a way to trace/monitor a variable at runtime (not for debugging) and throw certain events if it e.g. changes or falls out of the scope. Is there a .NET built-in way to do this? Like, not watching the variable itself and holding a reference to it, but the place in memory where it is allocated?
Ugly pseudo code:
public class Dummy {
public void DummyMethod(int randomVar) {
randomVar = 4;
}
}
public class MyApp {
static void Main() {
var testVar = 1;
SuperVariableWatcher.Watch(testVar);
SuperVariableWatcher.NotifyOnChange += (sender, args) =>
_logger.Write("variable bla bla has changed.");
var dummy = new Dummy().DummyMethod(testVar);
// since Dummy changes testVar, NotifyOnChange would now
// be called, maybe with information about
// who has changed it, what value did it contain before
}
}
Of course I can hold a reference to it in SuperVariableWatcher
and check for changes every nano-second, but even if it would work fast and reliable, I would then be unable to see if the variable is ready to be garbage-collected (because of no references to it) since I'd be the one that holds the reference.
I can think of something like angularJS $watch
, however I'm not sure how it works internally.
Aucun commentaire:
Enregistrer un commentaire