Puis-je faire référence à des méthodes / paramètres étendus sans avoir à couler à partir de l'objet de classe de base renvoyé

StackOverflow https://stackoverflow.com/questions/2831036

Question

Existe-t-il pour ne pas avoir de "coulé" le top.First (). Value () Retour à "node", mais il le fait automatiquement supposer cela (par opposition à NodeBase), donc je vois alors des attributs étendus pour la classe I définir dans le nœud?

C'est là un moyen de dire:

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

par opposition à avoir à partir:

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

Merci

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



}
Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top