无法创建通用方法:“T"未找到 [英] Cannot create a generic method: "T" not found

查看:33
本文介绍了无法创建通用方法:“T"未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个返回通用列表 (List) 的方法,但我不断收到此错误消息:

<块引用>

找不到类型或命名空间名称T"(您是否缺少 using 指令或程序集引用?)

这是我的方法代码:

公共静态列表doQuery(字符串查询){SQLiteCommand com = new SQLiteCommand(query, SQLiteManager.connection);SQLiteDataReader reader = com.ExecuteReader(CommandBehavior.Default);而 (reader.Read()){//其他代码}}

为什么在这种情况下 T 不被识别为泛型类型?

解决方案

你需要告诉方法T"是什么,现在你的方法不知道 T 是什么.T 在编译时已知,语言不会当场确定类型.

这里是一个例子:static ListGetInitializedList(T value, int count)

此处参考:http://www.dotnetperls.com/generic-method

I am trying to implement a method that returns a generic list (List), but I keep getting this error message:

The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)

This is my method code:

public static List<T> doQuery(string query)
{
    SQLiteCommand com = new SQLiteCommand(query, SQLiteManager.connection);
    SQLiteDataReader reader = com.ExecuteReader(CommandBehavior.Default);
    while (reader.Read())
    {
        //other code
    }
}

Why is T not recognized as a generic Type in this situation?

解决方案

You need to tell what is "T" to the method, right now your method does not know what T is. T is known at compile time, the language does not figure out the type on the spot.

Here's an example: static List<T> GetInitializedList<T>(T value, int count)

Reference here: http://www.dotnetperls.com/generic-method

这篇关于无法创建通用方法:“T"未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆