2013-07-29 88 views
-1

了InterfaceA包含GetName方法
接口層次結構例如C#

InterfaceB實現了接口A和包含的getStatus方法

foreach(InterfaceA item in tempList.getList()){ 
if(item is Interface B) 
rootNode = new TreeNode(item.GetName); 
rootNode.Tag = item 
childNode = new TreeNode(item.GetStatus) <--**is this possible>?? or is there any solution on getting instance with child interface from the parent?** 
childNode.Tag = item 

} 
+1

你的問題很難理解。請花一些時間讓它更清晰可讀。 –

回答

0

甲容易的解決方案是使用as代替isas會做什麼是返回接口,如果它可以被轉換或null,如果它不能。

foreach(InterfaceA item in tempList.getList()) 
{ 
    rootNode = new TreeNode(item.GetName()); 
    rootNode.Tag = item 

    InterfaceB itemB = item as InterfaceB; 
    if(itemB != null) 
    { 
     childNode = new TreeNode(itemB.GetStatus()) 
     childNode.Tag = itemB 
    } 
} 
3

你必須使用顯式強制從時的參考子類/接口的接入方式父類/接口。當然,你必須確定演員是合法的,在你的代碼中由if(item is Interface B)控制。

childNode = new TreeNode(((InterfaceB)item).GetStatus()) 
+2

你需要一個額外的圓括號來使演員在'.'之前發生以調用函數:'TreeNode((((InterfaceB)item).GetStatus())' –

+0

固定爲偉大的正義) – dmay