I'd like to know if it is possible to do the following:
using (MyClass o = new MyClass())
{
TheClassIWantMyClassToSee x = new TheClassIWantMyClassToSee();
x.DoStuff();
}
I'd like to create a class (MyClass) and use it in a using block. Inside this block, I want to work with objects of a certain type (TheClassIWantMyClassToSee). When using block falls out of scope, I want to perform certain actions on these (TheClassIWantMyClassToSee) objects.
Is it possible to make a class aware of other objects declared in its scope transparently?
I realise that I could add object instances to the MyClass object, but I'd like to make it easier for developers working with the API I'm building.
Any ideas are welcome.
Thanks.
The only way to make MyClass
aware of TheClassIWantMyClassToSee
is by creating a reference from one to the other. There is no way to navigate and explore the classes that are in scope. This statement is true regardless of whether the scope relates to a using block, method block, foreach loop or other.
Why not have a simple MyClass.AddRelationship(TheClassIWantMyClassToSee child)
method that makes this class aware of the other?