我试图在从表达式创建查询时复制实体框架的行为,并且在使用属性获取模型的属性时,我找到了使用 ExpressionVisitor 的方法
这是我到目前为止所得到的
internal class NVisitor : ExpressionVisitor
{
private readonly ParameterExpression _parameter;
private readonly Type _type;
public NVisitor(Type type)
{
_type = type;
_parameter = Expression.Parameter(type);
}
protected override Expression VisitParameter(ParameterExpression node)
{
return _parameter;
}
protected override Expression VisitMember(MemberExpression node)
{
if (node.Member.MemberType == MemberTypes.Property)
{
var memberName = node.Member.Name;
PropertyInfo otherMember = _type.GetProperty(memberName);
var ncols = node.Member.GetCustomAttributes(typeof(NColumn), true);
if (ncols.Any())
{
var ncol = (NColumn)ncols.First();
otherMember = _type.GetProperty(ncol.Name);
}
var inner = Visit(node.Expression);
return Expression.Property(inner, otherMember);
}
return base.VisitMember(node);
}
}
我有一个属性 NColumn ,它表示表格列中属性的真实名称,所以我按属性标记模型的属性
public class BonusTypeX
{
[NColumn("BonusTypeName")]
public string Name { get; set; }
}
现在当我试图获得表达时,
[TestMethod]
public void ExpressionTesting2()
{
string searchKey = "Xmas";
Expression<Func<BonusTypeX, bool>> expression = x => x.Name.Contains(searchKey);
Type t = typeof(tbl_BonusType);
var body = new NVisitor(t).Visit(expression.Body);
string a = string.Join(".", body.ToString().Split('.').Skip(1));
Assert.AreEqual("BonusTypeName.Contains(\"Xmas\")", a);
}
我得到了这个
BonusTypeName.Contains(值(Payroll.Test.Administration.TestRepositories + LT;&GT; c__DisplayClass13).searchKey)
我期待得到的是
BonusTypeName.Contains(&#34;圣诞&#34)
有没有获取表达式字符串的方法?我正在使用
string a = string.Join(".", body.ToString().Split('.').Skip(1));
我觉得这可能是错的.. :)
任何帮助将不胜感激。
答案 0 :(得分:5)
本地变量在运行时在编译器生成的类中捕获,这解释了Payroll.Test.Administration.TestRepositories+<>c__DisplayClass13).searchKey
部分。要在表达式中获取生成的字段值,您必须在访问表达式时明确替换它的值:
protected override Expression VisitMember(MemberExpression node)
{
if (node.Member.MemberType == MemberTypes.Property)
{
var memberName = node.Member.Name;
PropertyInfo otherMember = _type.GetProperty(memberName);
var ncols = node.Member.GetCustomAttributes(typeof(NColumn), true);
if (ncols.Any())
{
var ncol = (NColumn)ncols.First();
otherMember = _type.GetProperty(ncol.Name);
}
var inner = Visit(node.Expression);
return Expression.Property(inner, otherMember);
}
if (node.Member.MemberType == MemberTypes.Field)
{
if (node.Expression is ConstantExpression)
{
var owner = ((ConstantExpression)node.Expression).Value;
var value = Expression.Constant(((FieldInfo)node.Member).GetValue(owner));
return value;
}
}
return base.VisitMember(node);
}