C# Ways to access containing class without passing reference -


is there way access containing/enclosing class during initialization (constructor logic) of nested owned class without needing reference passed constructor parameter?

the reason don't want pass reference containing class constructor parameter nested owned class because virtually every single object in program need it, , feels sloppy passing in argument manually every time never change anyways. want simplify programming as possible other team members of mine using engine.

i tried making method container class use when adding new objects take new instance parameter , set instance's "container" variable "this" (the container class), nested owned object's initialization code happens first defeats purpose (i want able access container during initialization, container variable needs set before, not after constructor code executed).

any ways make happen? or doomed manually pass in reference container class every time make new nested owned object?

edit:

example: let's i'm making video game, , player going cast magic spell spawns healergoblin:

from player class:

spawnmonster(new healergoblin(30)); //30 = monster's combat level 

in healergoblin class:

public healergoblin(int level) {     owner.health += level; //owner in case, being player. } 

as see, player cast spawn monster spell, select level 30 healer goblin, , healer goblin has special effect when spawns increases owner's health amount (in case equal goblin's level). (keep in mind hypothetical, know there other, better, ways specifically, example of i'm trying do).

so problem is, player or npc in game cast spell , "owner". how goblin's owner property set time initialization code references owner executed?

i know pass owner reference:

spawnmonster(new healergoblin(this, 30); //first argument being owner set to. 

but want every single object in program automatically have reference "owners", , manually putting (this) every time initialize new object, , having set new owner parameter passed every time make new derived class, seems sloppy , counter-intuitive considering never not pass reference.

so question is other ways there reference container/owner object before initialization besides passing reference through constructor? if any?

i understand pain. i've been there too. answer no in standard way of programming. maybe possible using call stack, reflection , hack work, since trying simplify code, don't want have kind of stuff in code.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -