【发布时间】:2015-05-08 15:50:38
【问题描述】:
我有一个规范接口和类:
public interface ISpecification<TEntity> where TEntity : class, new()
{
Expression<Func<TEntity, bool>> SatisfiedBy();
}
public abstract class Specification<TEntity> : ISpecification<TEntity> where TEntity : class, new()
{
public abstract Expression<Func<TEntity, bool>> SatisfiedBy();
public static Specification<TEntity> operator &(Specification<TEntity> leftSideSpecification, Specification<TEntity> rightSideSpecification)
{
return new AndSpecification<TEntity>(leftSideSpecification, rightSideSpecification);
}
public static Specification<TEntity> operator |(Specification<TEntity> leftSideSpecification, Specification<TEntity> rightSideSpecification)
{
return new OrSpecification<TEntity>(leftSideSpecification, rightSideSpecification);
}
public static Specification<TEntity> operator !(Specification<TEntity> specification)
{
return new NotSpecification<TEntity>(specification);
}
public static bool operator false(Specification<TEntity> specification)
{
return false;
}
public static bool operator true(Specification<TEntity> specification)
{
return true;
}
}
public class Table1BySpecification: Specification<Table1>
{
Propiedades Privadas
Expression<Func<Table1, bool>> _Expression = null;
public Table1BySpecification(Expression<Func<Table1, bool>> expression)
{
this._Expression = expression;
}
public override Expression<Func<Table1, bool>> SatisfiedBy()
{
return this._Expression;
}
}
我创建了一个 Table1BySpecification 以便我可以从一个通用函数运行...
public IEnumerable<TEntity> GetBySpecification(string propertyName, object propertyValue, propertyOperator ){
ISpecification<Table1> Specification = new Table1BySpecification(t1 => t1.name == "test_name");
context.CreateDbSet<Table1>().Where(Specification.SatisfiedBy())
}
这适用于 Table1 实体,但是我想改用 TEntity,并使用反射来使用动态规范。也许我可以有类似的东西:
public IEnumerable<TEntity> GetBySpecification(string propertyName = "name", object propertyValue = "test_name", string propertyOperator = "==" ){
var source = typeof(TEntity);
var t = Expression.Parameter( source, "t" );
var sourceProperty = Expression.MakeMemberAccess( arg, source.GetProperty( propertyName ) );
PropertyInfo propertyField = type.GetProperty(propertyName);
Type propertyType = propertyField.GetType();
PropertyInfo[] props = typeof(TEntity).GetProperties();
foreach (PropertyInfo prop in props)
{
var displayAttribute = prop.GetCustomAttributes(false).FirstOrDefault(a => a.GetType() == propertyType);
if (displayAttribute != null)
{
var parameterExpression = Expression.Parameter(typeof(TEntity), "x");
var memberExpression = Expression.PropertyOrField(parameterExpression, prop.Name);
var memberExpressionConversion = Expression.Convert(memberExpression, typeof(object));
var lambda = Expression.Lambda<Func<TEntity, object>>(memberExpressionConversion, parameterExpression);
}
}
ISpecification<TEntity> Specification = new EntityByCustomSpecification<TEntity>(***Reflection_Expression***);
context.CreateDbSet<TEntity>().Where(Specification.SatisfiedBy()),
}
我不知道这段代码是否可以成为解决方案的一部分,以便至少在 propertyName 变量方面创建 (Reflection_Expression)。 我真的无法将 (Reflection_Expression) 创建为 lambda 表达式并最终获得该 Reflection_Expression 实现...
(t1 => t1.name == "test_name")
...当TEntity为t1时,propertyName为name,其值propertyValue为“test_name”,操作符propertyOperator为“==”
请帮忙???
非常感谢!
【问题讨论】:
标签: c# linq generics reflection expression