How can I identify down the call chain the encompassing using block? I mean without saving in a static or global the object created by using block nor passing it around. If I have:
using(new Foo())
{
A();
}
void A()
{
B();
}
inside function B I want to be able to identify and access Foo instance. It would be even nicer to get also the upper encompassing using block if any and so on.
There is no magic way to get ambient state, especially if you explicitly preclude things like static / async-local variables.
So: just pass it in. If
Bneeds to know theFoo, then:Bneeds to know theFoo; don't make that magic - make it explicit and simple by passing it in:(yes, I realize you also said "nor passing it around", but IMO that's the most appropriate solution)