0

我想动态创建一个更深层次的 MemberAcess 表达式(递归):

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 中的每个属性和 Employee 的复杂成员中的每个属性动态创建一个 MemberAccesExpressions 列表,结果应该是这样的:

MemberAccesExpression[] {
    { e => e.Name },
    { e => e.Job.Name },
    { e => e.Job.Name }
}

这是我得到的伪代码:

List list = new List();
public Expression<Func<TModel, dynamic>> CreateME<TModel>(TModel model)
{
        var type = typeof (TModel);
        var properties = type.GetProperties();
        foreach (var prop in properties)
        {
            // I want to ignore collections
            if (typeof(ICollection).IsAssignableFrom(prop.PropertyType)) continue;

            // Recall for complex property
            if (prop.PropertyType.Namespace != "System")
            {
                // CreateME(model, ) // THIS IS WHEN I DON'T KNOW WHAT TO DO
                continue;
            }

            var param = Expression.Parameter(type, "x");
            var memberAccess = Expression.PropertyOrField(param, prop.Name);
            list.Add(Expression.Lambda<Func<TModel, dynamic>>(memberAccess, param));
        }
}

我如何使它成为一种递归方法?我想添加一个名为 (TModel model, Expression> baseMemberAccess = null) 的可选参数,如果它不为 null,则以某种方式将成员表达式连接到 baseMemberAccess。

PS是否有更好的方法来确定 Type 是否不是原子类型然后 this (prop.PropertyType.Namespace != "System") ?(不是 int、float、string 等...)

感谢任何帮助,亚当

试图更简单地放置它的编辑:

如果我想创建一个成员访问 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 的成员访问,这相当于什么?

4

1 回答 1

2
public IEnumerable<Expression<Func<TModel, dynamic>>> CreateME<TModel>()
    {
        var stack = new Stack<StackItem>();
        var type = typeof(TModel);
        var parameterExpression = Expression.Parameter(type, "x");
        stack.Push(new StackItem(typeof(TModel), parameterExpression));

        while (stack.Count > 0)
        {
            var currentItem = stack.Pop();
            var properties = currentItem.PropertyType.GetProperties();
            foreach (var property in properties)
            {
                if (IsComplexProperty(property))
                    stack.Push(new StackItem(property.PropertyType, Expression.PropertyOrField(currentItem.AccessChainExpression, property.Name)));
                else
                {
                    yield return GetSimplePropertyExpression<TModel>(parameterExpression, currentItem.AccessChainExpression, property.Name);
                }
            }
        }


    }

    private static Expression<Func<TModel, dynamic>> GetSimplePropertyExpression<TModel>(ParameterExpression lhs, Expression accessChain, string propertyName)
    {
        var memberAccess = Expression.Convert(Expression.PropertyOrField(accessChain, propertyName), typeof(object));
        return Expression.Lambda<Func<TModel, dynamic>>(memberAccess, lhs);
    }

    private static bool IsComplexProperty(PropertyInfo p)
    {
        return !typeof (ICollection).IsAssignableFrom(p.PropertyType) && p.PropertyType.Namespace != "System";
    }


    class StackItem
    {
        public StackItem(Type propertyType, Expression accessChainExpression)
        {
            PropertyType = propertyType;
            AccessChainExpression = accessChainExpression;
        }

        public Type PropertyType { get; private set; }
        public Expression AccessChainExpression { get; private set; }
    }

我相信它可以改进,但这应该工作。

于 2012-06-22T19:34:59.073 回答