Please see my example code:
var testObject = new SomeClass();
using (testObject)
{
//At this point how can the testObject implicitly know
//if it is placed inside a 'using' scope?
// In other words, how can testObject know that
// .SomeAction() is being called from within a 'using' scope?
testObject.someAction();
}
It can’t.
It could get a stack dump to determine where the calling code is, and analyze the code to try to determine what it does. It could look for the
try...finalyanddisposethat theusingblock generates, but it could still not tell if it actually was ausingblock or not.