【发布时间】:2011-07-15 17:27:53
【问题描述】:
这是一堂课
public class Repository<T>
{
T GetSingle(Expression<Func<T, bool>> condition);
}
然后在另一个接受泛型类型参数的类中,我有类似的东西:
repo = new Repository<TEntity>();
repo.GetSingle(x=> x.Id == 1);
// That won't compile because TEntity is a generic type.
//Compiler doesn't know if TEntity has Id or not.
那么,如何传递那个表达式?
UPD:创建类型约束类似乎是合理的解决方案。但不幸的是对我不起作用。在我的例子中,TEntity 是一个实体框架的 EntityObject。即使我尝试创建一个约束类并从 EntityObject 或 StructuralObject 派生它,编译器也会说:没有隐式引用转换
【问题讨论】:
-
可以添加TEntity的代码定义吗?
标签: generics c#-4.0 lambda expression