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

Posted by Greg on Stack Overflow See other posts from Stack Overflow or by Greg
Published on 2010-05-13T23:56:49Z Indexed on 2010/05/14 0:04 UTC
Read the original article Hit count: 330

Hi,

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()
    {
    }

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



}

© Stack Overflow or respective owner

Related posts about c#

Related posts about object-oriented