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

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

问题描述

我想实现返回一个通用的清单(目录)的方法,但我不断收到此错误信息:

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

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

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

这是我的方法code:

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
    }
}

为什么为T不承认在这种情况下,一个通用的类型?

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

推荐答案

您需要告诉什么是T的方法,现在你的方法不知道T是什么。 T已知在编译的时候,语言不弄清楚当场类型。

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.

下面是一个例子:静态列表&LT; T&GT; GetInitializedList&LT; T&GT;(吨价,诠释计数)

在这里参考: http://www.dotnetperls.com/generic-method

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

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