Как мне добиться чего-то похожего на следующий пример?
public interface IGenericRepository {
int id { get; }
T GetById<T>() where T : class
}
public class GenericRepository : IGenericRepository {
//Some code here
public T GetById<T>(int tid) where T : class
{
return from tbl in dataContext.GetTable<T> where tbl.id == tid select tbl;
}
}
и я хотел бы использовать это следующим образом:
GenericRepository gr = new GenericRepository();
Category cat = gr.GetById<Category>(15);
Конечно, этот код не работает, так как если я определю T : class
, то часть tbl.id
не будет работать. Но, конечно, должен быть способ понять это.
UPDATE
Учитывая ответ Драйса, я делаю следующее, но все еще не могу заставить это работать:
public interface IEntity
{
int id { get; }
}
public interface IGenericRepository : IEntity
{
T GetById<T>(int id);
}
public class GenericRepository : IGenericRepository
{
public T GetById<T>(int id) {
return from tbl in dataContext.GetTable<T> where tbl.id == tid select tbl;
}
}
На данный момент tbl.id
работает, но dataContext.GetTable<T>
дает мне ошибку.