2010-05-13 62 views
0

有沒有一個「投」的頂部.First().VAL()返回到「節點」,而是有它會自動假設這個(而不是NodeBase),所以我會看到我在Node中定義的類的擴展屬性?可以參考擴展方法/參數而不必從返回的基類對象返回

也就是說有沒有辦法說:

top.Nodes.First().Value.Path; 

,而不是現在不必去:

((Node)top.Nodes.First().Value).Path) 

感謝

[TestMethod()] 
public void CreateNoteTest() 
{ 
    var top = new Topology(); 
    Node node = top.CreateNode("a"); 
    node.Path = "testpath"; 

    Assert.AreEqual("testpath", ((Node)top.Nodes.First().Value).Path); // *** HERE *** 
} 


class Topology : TopologyBase<string, Node, Relationship> 
{ 
} 

class Node : NodeBase<string> 
{ 
    public string Path { get; set; } 
} 


public class NodeBase<T> 
{ 
    public T Key { get; set; } 

    public NodeBase() 
    { 
    } 

    public NodeBase(T key) 
    { 
     Key = key; 
    }  


} 

public class TopologyBase<TKey, TNode, TRelationship> 
    where TNode : NodeBase<TKey>, new() 
    where TRelationship : RelationshipBase<TKey>, new() 

{ 
    // Properties 
    public Dictionary<TKey, NodeBase<TKey>> Nodes { get; private set; } 
    public List<RelationshipBase<TKey>> Relationships { get; private set; } 



} 

回答

2

TopologyBase,改變你的字典的TValueTNode而不是NodeBase<TKey>top.Nodes.First().Value將在您的示例代碼中返回一個Node