首页 > 解决方案 > C# 中的表达式树和惰性求值

问题描述

我有一小段代码,我在其中获取一个ParameterExpression字符串数组并将特定索引转换为目标类型。我通过调用Parse(如果类型是原始类型)或尝试进行原始转换(希望是字符串或隐式字符串)来做到这一点。

static Expression ParseOrConvert(ParameterExpression strArray, ConstantExpression index, ParameterInfo paramInfo)
{
    Type paramType = paramInfo.ParameterType;

    Expression paramValue = Expression.ArrayIndex(strArray, index);

    if (paramType.IsPrimitive) {
        MethodInfo parseMethod = paramType.GetMethod("Parse", new[] { typeof(string) });
        // Fetch Int32.Parse(), etc.

        // Parse paramValue (a string) to target type
        paramValue = Expression.Call(parseMethod, paramValue);
    }
    else {
        // Else attempt a raw conversion
        paramValue = Expression.Convert(paramValue, paramType);
    }

    return paramValue;
}

这行得通,但我正在尝试这样重写条件。

paramValue = Expression.Condition(
    Expression.Constant(paramType.IsPrimitive),
    Expression.Call(parseMethod, paramValue),
    Expression.Convert(paramValue, paramType)
);

这总是导致System.InvalidOperationException,大概是因为它尝试了两种转换。我发现第二种风格写起来更直观,所以这很不幸。

我可以以将评估推迟到实际需要这些值的方式来编写它吗?

标签: c#lazy-evaluationexpression-trees

解决方案


调试通常就像新闻业……在新闻业中有五个 W什么哪里何时为什么(加上如何)……在编程中它是相似的。抛出异常(那是什么)?让我们让代码更容易调试:

static Expression ParseOrConvert(ParameterExpression strArray, ConstantExpression index, ParameterInfo paramInfo)
{
    Type paramType = paramInfo.ParameterType;

    Expression paramValue = Expression.ArrayIndex(strArray, index);
    MethodInfo parseMethod = paramType.GetMethod("Parse", new[] { typeof(string) });

    var isPrimitive = Expression.Constant(paramType.IsPrimitive);
    var call = Expression.Call(parseMethod, paramValue);
    var convert = Expression.Convert(paramValue, paramType);

    var paramValue2 = Expression.Condition(
        isPrimitive,
        call,
        convert
    );

    return paramValue2;
}

然后这样称呼它:

public static void MyMethod(int par1)
{
}

接着

ParameterExpression strArray = Expression.Parameter(typeof(string[]));

// paramType int
var paramInfo = typeof(Program).GetMethod("MyMethod").GetParameters()[0];

var result = ParseOrConvert(strArray, Expression.Constant(0), paramInfo);

现在......抛出异常?Expression.Convert(paramValue, paramType)抛出异常......为什么?因为您正在尝试执行以下操作:

string paramValue = ...;
convert = (int)paramValue;

那肯定是违法的!即使是“死”代码(无法访问的代码)也必须在 .NET(以其 IL 语言)中“可编译”。所以你的错误是试图在你的表达式中引入一些非法的死代码,那就是:

string paramValue = ...;
isPrimitive = true ? int.Parse(paramValue) : (int)paramValue;

这不会在 C# 中编译,甚至可能无法用 IL 代码编写。并且 Expression 类抛出它。


推荐阅读