简体   繁体   English

通用继承c#

[英]Generic Inheritance c#

I have create a generic Node class which will be used to build a tree object. 我已经创建了一个通用的Node类,它将用于构建一个树对象。 I want to inherit the attributes of this class in another. 我想在另一个类中继承这个类的属性。 The job class represents a SQL job which is included in a chain (tree) of jobs. 作业类表示包含在作业链(树)中的SQL作业。 The following code is giving me an error and I am not sure why. 以下代码给我一个错误,我不知道为什么。

public class Node<T>
{
    public int id { get; set; }
    public Node<T> parent { get; set; }
    public List<Node<T>> Children = new List<Node<T>>();

    public bool isRoot
    {
        get { return parent == null; }
    }

    public static Node<T> createTree(List<Node<T>> nodes)
    {
        if (nodes.Count() == 0)
            return new Node<T>();
     //Build parent / Child relationships
    }
}

public class Job : Node<Job>
{
    public string name {get; set;}

    public Job(String name)
    {
        this.name = name;
    }
}


   List<Job> joblist = JobDict.Select(j => new Job(j.Key)).ToList();
   Node<Job>.createTree(joblist);

I am Unable to call createTree with the List of Jobs. 我无法使用作业列表调用createTree。 I realize changing it from List < Job > to List < Node< Job > > works but why am I unable to do the former? 我意识到将它从List <Job>更改为List <Node <Job >>>但是为什么我无法做前者? I figured because I am inheriting the node class, a List of Jobs would in fact be equivalent to a List of Node. 我想因为我继承了节点类,所以作业列表实际上等同于节点列表。 I am sorry if this is a very basic question but I just began with generics and inheritance and am having a hard time grasping it entirely. 我很抱歉,如果这是一个非常基本的问题,但我刚开始使用泛型和继承,并且很难完全掌握它。

The problem is that List<Node<Job>> and List<Job> are not co-variant. 问题是List<Node<Job>>List<Job>不是共变体。

If you're using .NET 4 you can do this. 如果您使用的是.NET 4,则可以执行此操作。

Node<Job>.createTree((IEnumerable<Node<Job>>)joblist);

or, you can modify the creeatetree method definition as follows. 或者,您可以按如下方式修改creeatetree方法定义。

public static Node<T> createTree(IList  nodes)
{
    if (nodes.Count == 0)
    return new Node<T>();       

    //Build parent / Child relationships
} 

I realize changing it from List<Job> to List<Node<Job>> works but why am I unable to do the former? 我意识到将它从List<Job>更改为List<Node<Job>>工作,但为什么我无法做前者?

Because List<Job> does not inherit List<Node<Job>> even if Job inherits Node<Job> . 因为即使Job继承Node<Job> List<Job>也不会继承List<Node<Job>> In other words, A inherits B does not mean List<A> inherits List<B> . 换句话说, A继承B并不意味着List<A>继承List<B>

You may need to cast each Job object to Node<Job> first: 您可能需要先将每个Job对象转换为Node<Job>

var jobNodeList = joblist.Select(j => (Node<Job>)j).ToList();

Node<Job>.createTree(jobNodeList);

This isn't a direct answer to the question, but it might give you some ideas on how to make your code simpler. 这不是问题的直接答案,但它可能会为您提供有关如何使代码更简单的一些想法。

It's often a good idea to keep code simple. 保持代码简单通常是一个好主意。 The simplest tree structure you can make is this: 你可以做的最简单的树结构是这样的:

public class Tree<T> : List<Tree<T>> { }

In your case, you would extend it slightly as: 在您的情况下,您会稍微扩展它:

public class Node<T> : List<Node<T>>
{
    public int id { get; set; }
    public Node<T> parent { get; set; }

    public bool isRoot
    {
        get { return parent == null; }
    }
}

Effectively you get all of the Children properties for free. 有效地,您可以免费获得所有Children酒店。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM