0

我正在尝试转换 ac# 表达式。这是我正在寻找的示例结果。兑换:

Expression<Func<TestObj, bool>> filter = p => p.LastName == "Smith";

对此:

Expression<Func<DynamicItem, bool>> converted = p => p.FieldID == 5 && p.FieldValue == "Smith"

正如您在下面的代码中看到的那样,FieldID 实际上是通过类上的属性的反射来获取的。我现在得到的错误是从 Convert 返回的错误是“'System.Int32' 类型的表达式不能用于返回类型'System.Boolean'”

我碰壁了,想也许有人可以看到我在这里做错了什么。这是我的代码:

public class MongoReplaceAttribute : Attribute
{
    public int FieldID { get; set; }
}

public class TestObj
{
    [MongoReplaceAttribute(FieldID = 1)]
    public string FirstName { get; set; }


    [MongoReplaceAttribute(FieldID = 2)]
    public string LastName { get; set; }
}

public class DynamicItem
{
    public int FieldID { get; set; }
    public string FieldValue { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        string v = "Smith";

        Expression<Func<TestObj, bool>> test = p => p.LastName == v;
        var list = MagicFunction(test);

        Console.ReadLine();
    }

    static IEnumerable<DynamicItem> MagicFunction(Expression<Func<TestObj, bool>> filter)
    {
        List<DynamicItem> rands = new List<DynamicItem>() 
        { 
            new DynamicItem 
            { 
                FieldID = 1, 
                FieldValue = "Bob" 
            }, 
            new DynamicItem 
            { 
                FieldID = 2, 
                FieldValue = "Smith" 
            },
            new DynamicItem 
            { 
                FieldID = 1, 
                FieldValue = "Alice" 
            }, 
            new DynamicItem 
            { 
                FieldID = 2, 
                FieldValue = "Smith" 
            } 
        };

        var f2 = Converter<DynamicItem>.Convert(filter);

        return rands.AsQueryable<DynamicItem>().Where(f2);
    }
}

class Converter<TTo>
{
    class ConversionVisitor : ExpressionVisitor
    {
        private readonly ParameterExpression newParameter;
        private readonly ParameterExpression oldParameter;

        public ConversionVisitor(ParameterExpression newParameter, ParameterExpression oldParameter)
        {
            this.newParameter = newParameter;
            this.oldParameter = oldParameter;
        }

        protected override Expression VisitLambda<T>(Expression<T> node)
        {
            return base.VisitLambda<T>(node);
        }

        protected override Expression VisitBinary(BinaryExpression node)
        {
            Expression left = this.Visit(node.Left);
            Expression right = this.Visit(node.Right);
            Expression conversion = this.Visit(node.Conversion);

            if (left != node.Left)
            {
                return base.Visit(left);
            }
            else if (right != node.Right)
            {
                var r = base.Visit(right);
                return r;
            }
            else
            {
                return node;
            }
        }

        protected override Expression VisitParameter(ParameterExpression node)
        {
            return newParameter;
        }

        protected override Expression VisitMember(MemberExpression node)
        {
            if (node.Expression != oldParameter)
            {
                return base.VisitMember(node);
            }

            var s = node.Member;
            var attrReader = s.GetCustomAttributes(typeof(MongoReplaceAttribute), true).Cast<MongoReplaceAttribute>();

            //If we are a mongo attribute
            if (attrReader != null && attrReader.Count() > 0)
            {
                var id = attrReader.First().FieldID;
                Expression<Func<DynamicItem, bool>> ret = p => p.FieldID == id && p.FieldValue == (string)GetValue(node);
                ParameterExpression param = Expression.Parameter(typeof(DynamicItem), "p");
                var lambda = Expression.Lambda<Func<DynamicItem, bool>>(ret.Body, param);
                return base.Visit(lambda.Body);
            }
            return base.VisitMember(node);
        }

        private object GetValue(MemberExpression member)
        {
            var objectMember = Expression.Convert(member, typeof(object));
            var getterLambda = Expression.Lambda<Func<object>>(objectMember);
            var getter = getterLambda.Compile();
            return getter();
        }
    }

    public static Expression<Func<TTo, TR>> Convert<TFrom, TR>(
        Expression<Func<TFrom, TR>> e)
    {
        var oldParameter = e.Parameters[0];
        var newParameter = Expression.Parameter(typeof(TTo), oldParameter.Name);
        var converter = new ConversionVisitor(newParameter, oldParameter);
        var newBody = converter.Visit(e.Body);
        return Expression.Lambda<Func<TTo, TR>>(newBody, newParameter);
    }
}
4

1 回答 1

0

在下面newBody的代码中,似乎没有正确创建。e.Bodyis p.LastName == "Smith" but newBodyisp.FieldID而不是 expected p.FieldId==5。注意缺少的相等比较。
因此,在最后一行中,当您尝试Func<DynamicItem, bool>从返回 int (p.FieldId) 的主体创建 Lambda 时,它会引发错误。

这很可能是由于 ConversionVisitor 中的一些错误。调试您的 ConversionVisitor 以查看它为什么会创建不正确的正文。

public static Expression<Func<TTo, TR>> Convert<TFrom, TR>(
    Expression<Func<TFrom, TR>> e)
{
    var oldParameter = e.Parameters[0];
    var newParameter = Expression.Parameter(typeof(TTo), oldParameter.Name);
    var converter = new ConversionVisitor(newParameter, oldParameter);
    var newBody = converter.Visit(e.Body);
    return Expression.Lambda<Func<TTo, TR>>(newBody, newParameter);
}
于 2013-02-17T19:42:20.637 回答