使用此泛型代码,为什么我会收到“参数 1:无法从“TopologyLibrary.RelationshipBase”转换?至“T关系”
有人知道为什么我在 CreateRelationship() 的下面的代码中收到“参数 1:无法从 'ToplogyLibrary.RelationshipBase' 转换为 'TRelationship'” 吗?
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;
}
}
Any see why I'm getting a "Argument 1: cannot convert from 'ToplogyLibrary.RelationshipBase' to 'TRelationship'" in the code below, in 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您对
TRelationship
的约束为RelationshipBase
。您可能是想说RelationshipBase
吗?Your constraint for
TRelationship
saysRelationshipBase<TKey>
. Did you perhaps mean it to sayRelationshipBase<TNode>
?使用这些行:
您并不是说 TRelationship = RelationshipBase,而是 TRelationship 继承自 RelationshipBase。
但是您不能将基类隐式转换为其后代。
那么,您确实需要这个:
或者
这对您来说就足够了?
或者也许查看你的代码:
你为什么不改变这一行:
与:
??
编辑:
正如 AakashM 所说,我认为你指的是 TNode 而不是 TKey
With these line:
You're not saying that TRelationship = RelationshipBase but TRelationship inherits from a RelationshipBase.
But you can't implicity convert a base class to its descendant.
So, you really need this:
or
this is enough for you ?
Or maybe looking your code:
why don't you change this line:
with:
??
EDIT:
as AakashM said I've supposed you meant TNode and not TKey