System.Reflection questions...

  • Thread starter Thread starter Alex
  • Start date Start date
A

Alex

Hi all...

Is there any way I can use reflection to get the complete "path" to the
current object?

So e.g. <objectA> creates <objectB> which creates <objectC>.

Then <objectB> executes a method provided by <objectC> which returns
something like "objectA.objectB.objectC".

Similarly, if <objectB> executes the same code on itself (assume it has the
same method defined as <objectC>", it would return "objectA.objectB"

Thanks!
Alex
 
I don't think you can. Otherwise even 'top level' objects you create, would
really be under something else. Also, what if objectC is referenced by
multiple objectB's? Maybe in your scenario that wouldn't happen, but you can
certainly imagine the it could in another, and then what would expect to get
for the path?

Typically if an object needs to know its parent, it will have a property for
this.
 
If you need objectA from objectC there is something wrong with your design
imo but i am not sure that this is what you mean. Anyway you can implement
your own ToString() and in each ToString() you return the base.ToString() +
current objects name

If C derives from B and B derives from A the ToString() of C would give
A.B.C

Gabriel Lozano-Morán
 
Marina,

Thanks - that's how I'm currently doing it (passing a string into the
constructor of each object, so it knows which parent object "owns" it).

I was just wondering if there was a neater way of doing it...

Alex
 
Back
Top