开发者

Can can I reference extended methods/params without having to cast from the base class object returned

Is there away to not have a "cast" the top.First().Value() return to "Node", but rather have it automatically assume this (as opposed to NodeBase), so I then see extended attributes for the class I define in Node?

That is is there a way to say:

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

as opposed to now having to go:

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

thanks

[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()
    {
    开发者_JS百科}

    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; }



}


In TopologyBase, change your dictionary's TValue to be TNode rather than NodeBase<TKey>. top.Nodes.First().Value will then return a Node in your sample code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜