c# - Expression.Switch() 在案例实现上苦苦挣扎

标签 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);

这将引发此异常:所有案例主体和默认主体必须具有相同的类型。

我认为这是指 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);

它引发异常:如果案例主体不是 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);

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

最佳答案

我自己找到了答案

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

使用此重载并传递 typeof(void) 时进入参数 type , 它会接受它。

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

这将完成工作:

// 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);

关于c# - Expression.Switch() 在案例实现上苦苦挣扎,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33825147/

相关文章:

http - Dreamweaver 通过 HTTP 连接到站点

python - Scrapy提取非包装数据

javascript - 使用正则表达式验证输入中是否包含任何非数字

c# - Accord.NET 中的回归分析

c# - 为什么 F# 表达式树的生成比 C# 慢得多,它可以更快吗?

c++ - 表达式是在C++中调用函数的结果

c# - UWP 蓝牙低功耗应用提前断开连接

c# - 在 View 或 Controller 中获取当前区域名称

c# - 为什么不能在 Page.PreInit 事件之后动态应用主题和母版页?

c# - RAD 编程语言编写一个 windows 应用程序来显示文本、播放音频文件和显示弹出气球