mardi 21 février 2023

Retrieving the reference of a class instance directly from one of its member methods that's passed as a parametre in a different class' method in C#

I'm developing a project in Unity. I think code sample will explain itself.

NOTES:

  1. Module constructor shall not need any other parameter, such as "this" referring to a Container instance

  2. Module constructor only parametre must stay as IEnumerator, not Func<> etc

  3. Coroutine passed as the parameter, will not be invoked directly (for ex. StartCoroutine(...) )

  4. Code logic that's inside the Module CTOR shall not be moved into a seperate method (neither public nor extension method).

     class Module
     {
             // CTOR
             public Module(IEnumerator _someCoroutine)
             {
             // This is where I need to get the reference to the
             // instance of a Container class (not "typeof(Container)")
             // which _someCoroutine method's reference is held, 
             // directly from _someCoroutine parameter
             }
    
     }
    
     class Container : MonoBehaviour
     {
             Module module = new Module(SomeCoroutine());
    
             IEnumerator SomeCoroutine()
             {...}
     }
    




Aucun commentaire:

Enregistrer un commentaire