4
我想創建一個泛型類T的通用查詢。有沒有辦法像那樣使用反射或其他東西?是否可以使用反射從通用實體訪問屬性(通過名稱)?
public class DAO<T>
where T : class
{
protected ObjectSet<T> Entities
{
get
{
return myContextThatIsInSomewhere.CreateObjectSet<T>();
}
}
public IList<T> SelectBy(object fields)
{
if (fields == null)
{
throw new ArgumentNullException("fields");
}
var query = from e in this.Entities
select e;
foreach (var field in fields.GetType().GetFields())
{
query = from e in this.Entities
// Do something like that:
where e.(field.Name) == field.GetValue()
select e;
}
return query.ToList();
}
}
謝謝賈森! –