简体   繁体   English

在方法中使用非抽象类型作为泛型类型的问题

[英]Problem with using a non-abstract type as a generic type in a method

I have this code that I am trying to use in my application: 我有尝试在我的应用程序中使用的以下代码:

public partial class DataManager 
{

    public DataManager()
    {
        db2 = DependencyService.Get<ISQLiteDB2>().GetConnection();
    }

    T RunQuery<T>(string qry)
    {
        lock (l)
        {
            try
            {
                T data = db2.Query<T>(qry);
                return data;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                Console.WriteLine(qry);
                throw;
            }
        }
    }

However the code is showing this error: 但是代码显示此错误:

Error CS0310: 'T' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method 'SQLiteConnection.Query(string, params object[])' 错误CS0310:“ T”必须是具有公共无参数构造函数的非抽象类型,以便在通用类型或方法“ SQLiteConnection.Query(string,params object [])”中将其用作参数“ T”

Can anyone give me advice on what this means and possibly how I can solve the problem. 任何人都可以就这意味着什么以及可能如何解决该问题给我建议。

You must use a Generic Constraint to satisfy the requirement and let the compiler know that the type passed in will always have a parameterless constructor, add where T: new() to the method definition. 您必须使用通用约束来满足要求,并让编译器知道传入的类型将始终具有无参数构造函数,请在方法定义的where T: new()添加。 Read more on generic constraints here https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/constraints-on-type-parameters 在此处阅读有关通用约束的更多信息https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/generics/constraints-on-type-parameters

public partial class DataManager 
{
    public DataManager()
    {
        db2 = DependencyService.Get<ISQLiteDB2>().GetConnection();
    }

    List<T> RunQuery<T>(string qry) where T: new()
    {
        lock (l)
        {
            try
            {
                List<T> data = db2.Query<T>(qry);
                return data;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                Console.WriteLine(qry);
                throw;
            }
        }
    }
}

暂无
暂无

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

相关问题 T必须是具有公共无参数构造函数的非抽象类型,以便在泛型类型或方法中将其用作参数“TModel” - T must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TModel' in the generic type or method 使用类型为String的Generic类并获取错误&#39;string&#39;必须是非抽象类型 - Using Generic class with type String and getting error 'string' must be non-abstract type 获取“T”必须是具有公共无参数构造函数的非抽象类型,以便在泛型类型或方法中将其用作参数“T” - Getting 'T' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method 通用 Class 加载程序:“T”必须是具有公共无参数构造函数的非抽象类型 - Generic Class Loader: 'T' must be a non-abstract type with a public parameterless constructor 'MvxWpfSetup<app> ' 必须是具有公共无参数构造函数的非抽象类型</app> - 'MvxWpfSetup<App>' must be a non-abstract type with a public parameterless constructor 必须是非抽象类型,并且在Redis中具有公共无参数构造函数 - must be a non-abstract type with a public parameterless constructor in redis 错误:“ T”必须是具有公共无参数构造函数的非抽象类型 - Error: 'T' must be a non-abstract type with a public parameterless constructor 必须是具有公共无参数构造函数的非抽象类型 .net 核心 - must be a non-abstract type with a public parameterless constructor .net Core 泛型类型的抽象方法 - Abstract method with generic type 在抽象类内的非抽象方法中调用抽象方法 - Calling an abstract method in a non-abstract method within an abstract class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM