Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi all,

I have an instance of a property A inside object B which inside object C etc.
And now I need to get a full path of property A => (C.B.A).

How can I do this? Thanx.
Posted

Hello,

Please check StackTrace Class.

@jafc
 
Share this answer
 
Comments
pin361 7-May-15 5:52am    
Can you please describe the solution with StackTrace class?
For example we have:

public class A { public SomeType v get {new SomeType(); } }
public class B { public A a {get {new A(); }} }
public class C { public B b {get {new B(); }} }

public class Test {
public void Foo(object prop) {
if(typeof(SomeType).IsAssignableFrom(prop.GetType()))
// Get prop path using StackTrace
};
public Test1() {
Test(new C().b.a.v);
}
}

Thank you
José Amílcar Casimiro 7-May-15 5:54am    
No i will not code for you.
I help you, giving you the way to get there.
Nope, you can't do what you seem to want.
Looking again at your example from the comment to José:
C#
public class A { public SomeType v get {new SomeType(); } }
public class B { public A a {get {new A(); }} }
public class C { public B b {get {new B(); }} }

public class Test { 
  public void Foo(object prop) { 
    if(typeof(SomeType).IsAssignableFrom(prop.GetType()))
      // Get prop path using StackTrace
  };
  // Assuming you actually meant this:
  public Test() {
    Foo(new C().b.a.v);
  }
}

When Foo is called it is passed an instance of SomeType, created in the getter of the v property of A.
That instance has absolutely no knowledge of who created it.
The information available to Foo is exactly the same as for:
C#
Foo(new SomeType());

Each of the property getters in your "chain" creates a new instance of an object and returns it before the next property getter is called so even StackTrace will not help.
The call "chain" in this case is sequential, not nested, so there's nothing useful in the stack.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900