使用ExpressionTree将MemberAcces创建为复杂类型

时间:2012-06-22 15:44:47

标签: c# linq lambda expression expression-trees

我想将MemberAcess表达式动态创建到更深层,然后是1(递归):

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模型,表达式&gt; baseMemberAccess = null) 并以某种方式将成员表达式连接到baseMemberAccess,如果它不是null。

P.S。 是否有更好的方法来确定Type是否不是原子类型     (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的成员访问权限相当于什么?

1 个答案:

答案 0 :(得分: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; }
    }

我确信它可以改进,但这应该有效。