2012-05-14 46 views
2

可以說我有這樣的事情:如何獲取屬性樹的跟蹤?

class Program 
{ 
    static void Main(string[] args) 
    { 
     A A1 = new A(); 
     A1.B1.C1.GetPath(); 
    } 
} 

class A 
{ 
    public B B1 { get; set; } 
    public B B2 { get; set; } 
} 

class B 
{ 
    public C C1 { get; set; } 
} 
class C 
{ 
    public string GetPath(); 
} 

是否有可能實現方法GetPath(),這將返回例如「C1 in B1 in A1」?

+0

相關:http://stackoverflow.com/questions/5716896/get-inheritance-tree-of-type –

回答

0

沒有內置的方法來解決這個問題。

可以做的是什麼:

  • OOP方式:定義一個通過父/ holder類參考構造函數,因此它可以在運行時想通了。

  • 使用Environment.StackTrace和得到公正的堆棧跟蹤(不知道這其實適合你的需要)

希望這有助於。