Lets say I have a few classes that looks a bit like these:
This class I'll call the parent instance:
public class Foo : Disposable {
public Foo() {
Bars = new List<Bar>();
FullPath = string.empty;
}
public Foo(string szInfo) {
Bars = new List<Bar>();
ImportantInfo = szInfo;
}
~Foo() => this.Dispose(false);
/* IDisposible stuff cropped for simplicity */
public string ImportantInfo {get; internal set;}
public List<Bar> Bars {get; internal set;}
public void SomeContainerLoadMethod() {
/* Add some bars here */
Bars.Add( new Bar() );
Bars.Add( new Bar() );
/* etc... */
}
}
As you can see here, the parent instance Foo
holds onto some Bar
classes.
I'll call these Bar
classes in the List<Bar>
the child instance containers in this question. Here's the definition of the Bar
class in the example code way:
public class Bar : Disposable {
Bar() { }
~Bar() => this.Dispose(false);
/* IDisposable stuff cropped for simplicity */
public string CoolBuff {get; internal set;}
public void SomeCoolStringBufMethod() {
/* Do something to populate CoolBuff, but I need ImportantInfo! */
}
}
How would I access ImportantInfo
from the parent instance , in the child instance container's SomeCoolStringBufMethod()
?
Here are the complications to this problem:
- Doing it without having to make a duplicate
ImportantInfo
property and pass it into the child instance container's constructor - Doing it without having to pass
ImportantInfo
in as an argument when the child instance'sSomeCoolStringBufMethod()
method is called from the parent .
Is this possible, say with System.Reflection
, to 'look up' the fact a Bar
is a member of a Foo
, and fetch Foo
's ImportantInfo
property ?