مع رمز الأدوية هذا لماذا أحصل على "الوسيطة 1: لا يمكنني التحويل من" toplogylibrary.relationshipbase إلى "trelationship" "
-
26-09-2019 - |
سؤال
أي رؤية لماذا أحصل على "الوسيطة 1: لا يمكنني التحويل من" toplogylibrary.relationshipbase "إلى" trelationship "في الكود أدناه ، في createrelationship ()؟
public class TopologyBase<TKey, TNode, TRelationship>
where TNode : NodeBase<TKey>, new()
where TRelationship : RelationshipBase<TKey>, new()
{
// Properties
public Dictionary<TKey, TNode> Nodes { get; private set; }
public List<TRelationship> Relationships { get; private set; }
// Constructors
protected TopologyBase()
{
Nodes = new Dictionary<TKey, TNode>();
Relationships = new List<TRelationship>();
}
// Methods
public TNode CreateNode(TKey key)
{
var node = new TNode {Key = key};
Nodes.Add(node.Key, node);
return node;
}
public void CreateRelationship(TNode parent, TNode child)
{
// Validation
if (!Nodes.ContainsKey(parent.Key) || !Nodes.ContainsKey(child.Key))
{
throw new ApplicationException("Can not create relationship as either parent or child was not in the graph: Parent:" + parent.Key + ", Child:" + child.Key);
}
// Add Relationship
var r = new RelationshipBase<TNode>();
r.Parent = parent;
r.Child = child;
Relationships.Add(r); // *** HERE *** "Argument 1: cannot convert from 'ToplogyLibrary.RelationshipBase<TNode>' to 'TRelationship'"
}
}
public class RelationshipBase<TNode>
{
public TNode Parent { get; set; }
public TNode Child { get; set; }
}
public class NodeBase<T>
{
public T Key { get; set; }
public NodeBase()
{
}
public NodeBase(T key)
{
Key = key;
}
}
المحلول
مع هذا الخط:
where TRelationship : RelationshipBase<TNode>, new()
أنت لا تقول أن trelationship = العلاقة بين العلاقة ولكن trelationship يرث من قاعدة العلاقة.
لكن لا يمكنك تحويل فئة أساسية إلى سليلها.
لذلك ، أنت حقا بحاجة إلى هذا:
List<TRelationship>
أو
List<RelationshipBase<TNode>>
هذا يكفي لك؟
أو ربما تبحث عن الكود الخاص بك: لماذا لا تغير هذا السطر:
var r = new RelationshipBase<TNode>();
مع:
var r = new TRelationship();
??
تحرير: كما قال Aakashm ، لقد افترضت أنك أقصد tnode وليس tkey
نصائح أخرى
القيد الخاص بك ل TRelationship
يقول RelationshipBase<TKey>
. هل تعني ذلك أن تقول RelationshipBase<TNode>
?
لا تنتمي إلى StackOverflow