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[])'
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. Read more on generic constraints here https://docs.microsoft.com/en-us/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;
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.