简体   繁体   English

表达式树 - 参数未从 lambda 函数参数映射到表达式参数

[英]Expression trees - parameter not mapped from lambda function argument to Expression Parameter

I am trying to wrap my head around Expression trees and I came to the below issue which I can not resolve.我试图围绕表达式树进行思考,但遇到了以下我无法解决的问题。 I am trying to generate a simple lambda function to check whether an integer value is even:我正在尝试生成一个简单的 lambda 函数来检查整数值是否为偶数:

        public static Func<int, bool> Generate_IsEven_Func()
        {
            var numParam = Expression.Parameter(typeof(int), "numParam");
            var returnValue = Expression.Variable(typeof(bool), "returnValue");

            var consoleWL = typeof(Console).GetMethod(nameof(Console.WriteLine), new[] { typeof(int) });
            
            var body = Expression.Block(
                 // Scoping the variables
                 new[] { numParam, returnValue },

                 // Printing the current value for numParam
                 Expression.Call(
                     null,
                     consoleWL,
                     numParam
                     ),

                 // Assign the default value to return
                 Expression.Assign(returnValue, Expression.Constant(false, typeof(bool))),

                 // If the numParam is even the returnValue becomes true
                 Expression.IfThen(
                            Expression.Equal(
                                Expression.Modulo(
                                    numParam,
                                    Expression.Constant(2, typeof(int))
                                    ),
                                Expression.Constant(0, typeof(int))
                                ),
                            Expression.Assign(returnValue, Expression.Constant(true, typeof(bool)))
                        ),

                 // value to return
                 returnValue
                );

            var lambda = Expression.Lambda<Func<int, bool>>(body, numParam).Compile();
            return lambda;
        }

When I call the newly created lambda function it appears that the value I pass as an argument is not 'mapped' with the respective expression parameter - numParam .当我调用新创建的 lambda 函数时,我作为参数传递的值似乎没有与相应的表达式参数 - numParam “映射”。 Within the block expression I make a call to the Console.WriteLine method to check the current value of numParam and it is 0 every time:在块表达式中,我调用Console.WriteLine方法来检查numParam的当前值,并且每次都为 0:

  var isEvenMethod = Generate_IsEven_Func();
  var cond = isEvenMethod(21);
  Console.WriteLine(cond);

  // Prints:
  // 0
  // True

You should not include numParam in the array of variables you're passing as the first parameter to Expression.Block(...) - right now you're essentially creating a new numParam variable within the block that is unrelated to the numParam parameter passed to the lambda.您不应该在作为第一个参数传递给Expression.Block(...)的变量数组中包含numParam - 现在您实际上是在块中创建一个新的numParam变量,该变量与传递的numParam参数无关到拉姆达。 Since the variable is automatically initialized with the default value for int (0), that explains the output you're seeing.由于变量使用int (0) 的默认值自动初始化,这就解释了您看到的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM