c# - Is it possible to detect the variable of a using from another function and access that variable? -
i have function calls function. want know if within second function can detect if being invoked first function within using-scope. if can detect it, want access variable inside using-scope. cannot send variable through parameter.
for example:
// normal call otherfunction void function1() { someclass.otherfunction(); } // calling using somevar void function2() { using(var somevar = new somevar()) { someclass.otherfunction(); } } // calling using other variable, in case, similar behaviour function1() void function3() { using(var anothervar = new anothervar()) { someclass.otherfunction(); } } class someclass { static void otherfunction() { // how know if being called inside using(somevar) // , access local variables somevar } }
you use same mechanisme system.transaction.transasctionscope. works if context's can have same base class. base class registerers himself in static property during construction , removes himself @ dispose. if context active, surpressed until newest context disposed again.
using system; namespace contextdemo { class program { static void main(string[] args) { detectcontext(); using (new contexta()) detectcontext(); using (new contextb()) detectcontext(); } static void detectcontext() { context context = context.current; if (context == null) console.writeline("no context"); else console.writeline("context of type: " + context.gettype()); } } public class context: idisposable { #region static members [threadstatic] static private context _current; static public context current { { return _current; } } #endregion private readonly context _previous; public context() { _previous = _current; _current = this; } public void dispose() { _current = _previous; } } public class contexta: context { } public class contextb : context { } }
Comments
Post a Comment