var param = Expression.Parameter(typeof(Employee), "t");
MemberExpression member = Expression.Property(param, "EmployeeName");
var value = Convert.ChangeType(filterProperty.Value, member.Type);
ConstantExpression constant = Expression.Constant(value);
var body = Expression.Or(leftExpr, Expression.Equal(member, constant));
我可以轻松获取正常属性的表达式,但 如何获取索引器属性的表达式?
在Employee
课程中,我有两个索引器。
class Employee
{
public string EmployeeName {get;set;}
public string this[EmployeeTypes empType]
{
get
{
return GetEmployee(empType);
}
}
public string this[int empNum]
{
get
{
return GetEmployee(empNum);
}
}
}
答案 0 :(得分:5)
使用Item
作为属性名称:
var param = Expression.Parameter(typeof(Employee), "t");
MemberExpression member = Expression.Property(param, "EmployeeName");
var body = Expression.Property(param, "Item", Expression.Constant(10));
var lambda = Expression.Lambda<Func<Employee, string>>(body, param);
var compiled = lambda.Compile();
为您提供与
相同的功能Func<Employee, string> compiled = t => t[10];