如何使用MethodCallExpression / lambda从树中的ConditionalExpression.IfThen返回?

问题描述 投票:4回答:1

我试图让一个表达式树有条件地评估一个字符串。

这是我到目前为止的代码:

IQueryable<Category> myCategories = DataUtil.Categories.AsQueryable();

ParameterExpression categoryParameterExpression = Expression.Parameter(typeof (Category), "category");
MemberExpression categoryNameMemberExpression = Expression.PropertyOrField(categoryParameterExpression, "CategoryName");
MemberExpression categoryNameLengthExpression = Expression.Property(categoryNameMemberExpression, typeof (string).GetProperty("Length"));
ConstantExpression constantLengthExpression = Expression.Constant(10, typeof (int));
BinaryExpression greaterThanLengthExpression = Expression.GreaterThan(categoryNameLengthExpression, constantLengthExpression);
var getNameInCapsMethod = typeof (Category).GetMethod("GetNameInCaps", BindingFlags.Instance | BindingFlags.Public);
MethodCallExpression getNameInCapsExpression = Expression.Call(categoryParameterExpression, getNameInCapsMethod, categoryNameMemberExpression);

ConditionalExpression ifGreaterThanLengthGetUpperNameExpression = Expression.IfThen(greaterThanLengthExpression, getNameInCapsExpression); 

// I need something between the lambda and the ConditionalExpression to ensure that the void type is not returned?

var ifGreaterThanLengthGetUpperNameLambdaExpression = Expression.Lambda<Func<Category, string>>(ifGreaterThanLengthGetUpperNameExpression, new ParameterExpression[] { categoryParameterExpression }); 
foreach (var category in myCategories)
{
    var upperName = ifGreaterThanLengthUpperLambda(category);
    System.Windows.MessageBox.Show(upperName);
}

这是运行时发生的ArgumentException:

System.Core.dll中发生了未处理的“System.ArgumentException”类型异常

附加信息:'System.Void'类型的表达式不能用于返回类型'System.String'

我已经发现ConditionalExpression返回“IfFalse”条件的void类型。

这是我的Expression Tree Visualizer的截图:

我只想要字符串值。我意识到在ConditionalExpression类型上有一个Expression.IfThenElse,但我不确定在Else表达式中放入什么。 (如果可能的话,我不想只传回一个空字符串。)有没有办法确保只在前面的BinaryExpression计算结果为true时才评估条件?无论如何,我该如何解决这个问题?

c# linq expression-trees
1个回答
13
投票

更充分地表达你真正想要实现的东西可能会有所帮助(基本上,你想要的表达式树的C#等价物是什么样的)。目前,您的表达式树看起来大致相当于:

Func<Category, string> f = (category) => { 
    if (category.CategoryName.Length > 10) {
        category.GetNameInCaps(category.CategoryName);
    }
};

但是,C#编译器不会编译它,因为你没有在任何代码路径上返回一个字符串,所以难怪表达式树没有按你的意愿编译。至少有两个问题:

  1. 你几乎肯定想要使用Expression.Condition而不是Expression.IfThen。这使您可以使用C#中的... ? ... : ...语法,它是一个可以用作lambda返回值的表达式。作为一个声明,if (...) { }块总是具有void类型,正如您所发现的(即使您添加了else也是如此)。
  2. 当你的情况不成立时,你需要弄清楚要做什么。如果你真的需要返回一个字符串,那么你必须要么 选择一些默认的无意义值来返回(例如null""),或 抛出一个例外
© www.soinside.com 2019 - 2024. All rights reserved.