基本クラスのオブジェクトからキャストすることなく、拡張されたメソッド/パラメーションを参照できますか
-
26-09-2019 - |
質問
top.first()。value()を「ノード」に戻す「キャスト」を持たないのに、むしろ(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; }
}
解決
の TopologyBase
, 、辞書を変更します TValue
することが TNode
それよりも NodeBase<TKey>
. top.Nodes.First().Value
その後、aを返します Node
サンプルコードで。
所属していません StackOverflow