I want to make a simple repository and underneath there is the method I have used:
public static List<T> GetAll()
{
return _dbConnection.Table<T>().ToList();
}
But I am encountering an error:
'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 'SQLite.SQLiteConnection.Table<T>()'
I use SQLite
wrapper sqlite-net
.
Ofc my class is generic:
class FeedRepository<T> : IFeedRepository<T>, IDisposable where T : IFeed
It sounds like you just need to add another constraint to T
- the Table<T>()
method appears to want the new()
constraint, so you'll need to add that to your constraint list too:
class FeedRepository<T> : IFeedRepository<T>, IDisposable where T : IFeed, new()
At that point, the compiler will know that the T
type parameter for your class satisfies the constraint for the T
type parameter of the Table
method, and it should be fine. Of course, this means that anyone using your FeedRepository<T>
class can only do so with non-abstract types with a public constructor... but then that's a natural limitation of what you're trying to achieve.
See more on this question at Stackoverflow