¿Puedo hacer referencia a métodos/parámetros extendidos sin tener que emitir desde el objeto de clase base devuelto?

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

Pregunta

¿Hay que no tener un "fundido" the Top.first ()? Value () Return a "Node", sino que lo asumiera automáticamente esto (a diferencia de NodeBase), por lo que veo atributos extendidos para la clase I definir en el nodo?

Esa es una manera de decir:

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

En lugar de tener que ahora:

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

Gracias

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



}
¿Fue útil?

Solución

En TopologyBase, cambia tu diccionario TValue ser - estar TNode más bien que NodeBase<TKey>. top.Nodes.First().Value Luego devolverá un Node en su código de muestra.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top