添加到Expression.Lambda 2条件取决于布尔返回结果

时间:2017-09-24 12:16:51

标签: c# expression

我有返回boolean的函数:

var lambdaFunc = Expression.Lambda<Func<T, bool>>(binaryExpression, genericType);

如何在函数中添加案例取决于bool答案,如果返回false我想添加log.debug("false"), 如果返回true,则添加log.debug("true")

构建函数的完整表达式树是:

                //Input parameter
                var genericType = Expression.Parameter(typeof(T));
                Expression value = null;
                //Get Comparison property
                var key = Expression.Property(genericType, rule.ComparisonPredicate);
                Type propertyType = typeof(T).GetProperty(rule.ComparisonPredicate).PropertyType;
                //convert case is it DateTimeOffset property
                if (propertyType == typeof(DateTimeOffset))
                {
                    var converter = TypeDescriptor.GetConverter(propertyType);
                    value = Expression.Constant((DateTimeOffset)converter.ConvertFromString(rule.ComparisonValue));
                }
                else
                {
                    value = Expression.Constant(Convert.ChangeType(rule.ComparisonValue, propertyType));
                }
                var binaryExpression = Expression.MakeBinary(rule.ComparisonOperator, key, value);
                var lambdaFunc = Expression.Lambda<Func<T, bool>>(binaryExpression, genericType);

0 个答案:

没有答案