public class Job
{
public string Name { get; set; }
public int Salary { get; set; }
}
public class Employee
{
public string Name { get; set; }
public Job Job { get; set; }
}
如果我想创建一个成员访问到Employee.Name的表达式目录树,这是我做的:
var param = Expression.Parameter(type, "x");
var memberAccess = Expression.PropertyOrField(param, memberName);
return Expression.Lambda<Func<TModel, TMember>>(memberAccess, param);
什么是相当于这一个成员访问Employee.Job.Salary?
你需要:
var jobProperty = Expression.PropertyOrField(param, "Job");
var salaryProperty = Expression.PropertyOrField(jobProperty, "Salary");
基本上你正在做的Salary
从评估的结果性质x.Job
。
如果你需要做这以编程的方式,你需要这样的:
Expression expression = Expression.Parameter(type, "x");
foreach (var property in properties.Split('.'))
{
expression = Expression.PropertyOrField(expression, property);
}
将创建扩展的最好方法如下 :
public static class ExpressionExtensions
{
/// <summary>
/// create expression by property name
/// </summary>
/// <typeparam name="TModel"></typeparam>
/// <param name="propertyName">
/// <example>Urer.Role.Name</example>
/// </param>
/// <returns></returns>
public static Expression<Func<TModel, dynamic>> CreateExpression<TModel>(this string propertyName) {
Type currentType = typeof (TModel);
ParameterExpression parameter = Expression.Parameter(currentType, "x");
Expression expression = parameter;
int i = 0;
List<string> propertyChain = propertyName.Split('.').ToList();
do {
System.Reflection.PropertyInfo propertyInfo = currentType.GetProperty(propertyChain[i]);
currentType = propertyInfo.PropertyType;
i++;
if (propertyChain.Count == i)
{
currentType = typeof (object);
}
expression = Expression.Convert(Expression.PropertyOrField(expression, propertyInfo.Name), currentType);
} while (propertyChain.Count > i);
return Expression.Lambda<Func<TModel, dynamic>>(expression, parameter);
}
}
您不能转换()来的typeof(对象),每次,因为System.Object的没有财产,那你的类型有(如姓名或薪水在你的例子)。