【问题标题】:The non-generic type 'System.Collections.IEnumerable' cannot be used with type arguments非泛型类型“System.Collections.IEnumerable”不能与类型参数一起使用
【发布时间】:2011-10-19 09:03:47
【问题描述】:
using System.Collections.Generic;

public sealed class LoLQueue<T> where T: class
{
    private SingleLinkNode<T> mHe;
    private SingleLinkNode<T> mTa;

    public LoLQueue()
    {
        this.mHe = new SingleLinkNode<T>();
        this.mTa = this.mHe;
    }
}

错误:

The non-generic type 'LoLQueue<T>.SingleLinkNode' cannot be used with type arguments

我为什么会得到这个?

【问题讨论】:

  • 你对 SingleLinkNode 的定义是什么?

标签: c# .net generics queue nodes


【解决方案1】:

如果您想使用IEnumerable&lt;T&gt;,正如您的帖子标题所示,您需要包含using System.Collections.Generic;

至于 SingleLinkNode 类,我不知道你从哪里弄来的,它不是我能看到的 .NET 框架的一部分。我猜它不是使用泛型实现的,你需要在任何地方添加一堆从 objectT 的演员表。

【讨论】:

  • 他已经在他的代码的第一行列出了那个。
  • 是的,但考虑到这个问题的标题是“非泛型类型 System.Collections.IEnumerable”,我认为代码的复制粘贴发生了一些变化。
  • 这为我解决了这个问题,但我不知道为什么我只需要在一台服务器上使用它,而不需要在其他服务器上使用它。是否在某处的服务器级别设置了一些默认值?
【解决方案2】:

我很确定您没有将您的 SingleLinkNode 类定义为具有泛型类型参数。因此,尝试用 one 声明它失败了。

错误消息表明 SingleLinkNode 是一个嵌套类,所以我怀疑可能发生的情况是您声明了 T 类型的 SingleLinkNode 的成员,而实际上没有将 T 声明为SingleLinkNode。如果您希望SingleLinkNode 是通用的,您仍然需要这样做,但如果不是,那么您可以简单地将类用作SingleLinkNode 而不是SingleLinkNode&lt;T&gt;

我的意思的例子:

public class Generic<T> where T : class
{
    private class Node
    {
        public T data; // T will be of the type use to construct Generic<T>
    }

    private Node myNode;  // No need for Node<T>
}

如果您确实希望您的嵌套类是通用的,那么这将起作用:

public class Generic<T> where T : class
{
    private class Node<U>
    {
        public U data; // U can be anything
    }

    private Node<T> myNode;  // U will be of type T
}

【讨论】:

    【解决方案3】:

    这是为我编译的:

    public sealed class SingleLinkNode<T>
    {
    
    }
    
    public sealed class LoLQueue<T> where T : class
    {
        private SingleLinkNode<T> mHe;
        private SingleLinkNode<T> mTa;
    
        public LoLQueue()
        {
            this.mHe = new SingleLinkNode<T>();
            this.mTa = this.mHe;
        }
    }
    

    您需要发布您的 SingleLinkNode 课程以获得更多答案...

    约翰

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-03
      • 2014-04-20
      相关资源
      最近更新 更多