使用此泛型代码,为什么我会收到“参数 1:无法从“TopologyLibrary.RelationshipBase”转换?至“T关系”

发布于 2024-09-01 16:15:26 字数 1749 浏览 1 评论 0原文

有人知道为什么我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幸福丶如此 2024-09-08 16:15:26

您对 TRelationship 的约束为 RelationshipBase。您可能是想说 RelationshipBase 吗?

Your constraint for TRelationship says RelationshipBase<TKey>. Did you perhaps mean it to say RelationshipBase<TNode> ?

北城孤痞 2024-09-08 16:15:26

使用这些行:

where TRelationship : RelationshipBase<TNode>, new()

您并不是说 TRelationship = RelationshipBase,而是 TRelationship 继承自 RelationshipBase。

但是您不能将基类隐式转换为其后代。

那么,您确实需要这个:

List<TRelationship>

或者

List<RelationshipBase<TNode>>

这对您来说就足够了?

或者也许查看你的代码:
你为什么不改变这一行:

var r = new RelationshipBase<TNode>();

与:

var r = new TRelationship();

??

编辑:
正如 AakashM 所说,我认为你指的是 TNode 而不是 TKey

With these line:

where TRelationship : RelationshipBase<TNode>, new()

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:

List<TRelationship>

or

List<RelationshipBase<TNode>>

this is enough for you ?

Or maybe looking your code:
why don't you change this line:

var r = new RelationshipBase<TNode>();

with:

var r = new TRelationship();

??

EDIT:
as AakashM said I've supposed you meant TNode and not TKey

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文