هل يمكنني الرجوع إلى الأساليب/المعاملات الممتدة دون الحاجة إلى إرجاع كائن الفئة الأساسية
-
26-09-2019 - |
سؤال
هل هناك بعيدا عن عدم وجود "يلقي" أعلى. تعريف في العقدة؟
هذا هو هناك طريقة لقول:
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
سيعود بعد ذلك Node
في نموذج رمزك.
لا تنتمي إلى StackOverflow