Question

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));

I can easily get the expressions for normal properties, but How I can get expression for indexer properties?

In Employee class I have two indexers.

    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);
           }
        }
    }
Était-ce utile?

La solution

Use Item as property name:

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();

gives you the same what could be done by

Func<Employee, string> compiled = t => t[10];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top