0

是否没有“强制转换”top.First().Value() 返回到“Node”,而是让它自动假设这个(而不是 NodeBase),所以我然后看到类 I 的扩展属性在节点中定义?

那就是有办法说:

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



}
4

1 回答 1

2

TopologyBase中,将您的字典更改TValueTNode而不是NodeBase<TKey>top.Nodes.First().Value然后将Node在您的示例代码中返回 a 。

于 2010-05-14T00:11:26.137 回答