Expression.Switch() 在案例实现上遇到困难

Jer*_*gen 4 c# expression

我正在尝试实现Expression.Switch()在表达式中创建 switch 语句。我正在努力解决一些问题。

我创建了一个引发异常的小示例。开关使用字符串来切换大小写,并在该大小写内分配一个变量。

下面是一些会引发异常的代码:

// an int variable
var var1 = Expression.Parameter(typeof(int));
// a string variable
var var2 = Expression.Parameter(typeof(string));

var cases = new[] 
{ 
    // the first case 'SwitchCaseValue1' should write the int value 1 into variable var1
    Expression.SwitchCase(Expression.Assign(var1, Expression.Constant(1)), Expression.Constant("SwitchCaseValue1")),
    // the second case 'SwitchCaseValue2' should write the string value 'Example' into variable var2 
    Expression.SwitchCase(Expression.Assign(var2, Expression.Constant("Example")), Expression.Constant("SwitchCaseValue2"))
};

Expression.Switch(Expression.Constant("SwitchValue"), cases);
Run Code Online (Sandbox Code Playgroud)

这将引发此异常:All case bodies and the default body must have the same type.

我认为这指的是Assign在情况 1 中返回一个 int,在情况 2 中返回一个字符串。我怎样才能不返回任何内容?


如果我将其更改var2为 int 类型:

// an int variable
var var1 = Expression.Parameter(typeof(int));
// a string variable
var var2 = Expression.Parameter(typeof(int));

var cases = new[] 
{ 
    // the first case 'SwitchCaseValue1' should write the int value 1 into variable var1
    Expression.SwitchCase(Expression.Assign(var1, Expression.Constant(1)), Expression.Constant("SwitchCaseValue1")),
    // the second case 'SwitchCaseValue2' should write the string value 'Example' into variable var2 
    Expression.SwitchCase(Expression.Assign(var2, Expression.Constant(2)), Expression.Constant("SwitchCaseValue2"))
};

Expression.Switch(Expression.Constant("SwitchValue"), cases);
Run Code Online (Sandbox Code Playgroud)

它引发异常:Default body must be supplied if case bodies are not System.Void.

所以我改变了:最后一行 Expression.Switch(Expression.Constant("SwitchValue"), Expression.Constant(0), cases);

这将编译。但这不是我要找的......


是否有另一种方法可以修复第一个解决方案,而无需实现额外的代码来返回相同的类型?

我不喜欢选择无意义的返回值之类的解决方案,例如:

// an int variable
var var1 = Expression.Parameter(typeof(int));
// a string variable
var var2 = Expression.Parameter(typeof(string));

var cases = new[] 
{ 
    // the first case 'SwitchCaseValue1' should write the int value 1 into variable var1
    Expression.SwitchCase(
        Expression.Block( 
            Expression.Assign(var1, Expression.Constant(1)), 
            // return a boolean
            Expression.Constant(true)), 
        Expression.Constant("SwitchCaseValue1")),
    // the second case 'SwitchCaseValue2' should write the string value 'Example' into variable var2 
    Expression.SwitchCase(
        Expression.Block( 
            Expression.Assign(var2, Expression.Constant("Example")), 
            // return a boolean
            Expression.Constant(true)), 
        Expression.Constant("SwitchCaseValue2"))
};


Expression.Switch(Expression.Constant("SwitchValue"), Expression.Constant(false), cases);
Run Code Online (Sandbox Code Playgroud)

我理解表达式和返回值的逻辑。但是有没有办法避免实现无意义的代码呢?

Jer*_*gen 6

我自己找到了答案

Expression.Switch()Switch(Type type, Expression switchValue, Expression defaultBody, MethodInfo comparison, IEnumerable<SwitchCase> cases);

当使用此重载并传递typeof(void)给参数时type,它将接受它。

msdn:“SwitchExpression 对象中的所有 SwitchCase 对象必须具有相同的类型,除非 SwitchExpression 的类型为 void。” 我没有在SwitchExpression 类型和包含类型参数的重载之间建立联系。

这将完成这项工作:

// an int variable
var var1 = Expression.Parameter(typeof(int));
// a string variable
var var2 = Expression.Parameter(typeof(string));

var cases = new[] 
{ 
    // the first case 'SwitchCaseValue1' should write the int value 1 into variable var1
    Expression.SwitchCase(
            Expression.Assign(var1, Expression.Constant(1)), 
            Expression.Constant("SwitchCaseValue1")),
    // the second case 'SwitchCaseValue2' should write the string value 'Example' into variable var2 
    Expression.SwitchCase(
            Expression.Assign(var2, Expression.Constant("Example")), 
            Expression.Constant("SwitchCaseValue2"))
};

Expression.Switch(typeof(void), Expression.Constant("SwitchValue"), null, null, cases);
Run Code Online (Sandbox Code Playgroud)