Expression.Switch() 在案例实施上苦苦挣扎
Expression.Switch() struggling on case implementation
我正在尝试实施 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);
这将引发此异常:All case bodies and the default body must have the same type.
我认为这指的是 Assign
returning 对于情况 1 是一个 int,对于情况 2 是一个字符串。 我怎么能return什么都没有?
如果我将 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);
引发异常:Default body must be supplied if case bodies are not System.Void.
所以我改了:最后一行改为
Expression.Switch(Expression.Constant("SwitchValue"), Expression.Constant(0), cases);
这将编译。但不是我要找的...
是否有另一种方法可以解决第一个解决方案,而无需对 return 相同类型实施额外的代码?
我不喜欢选择无意义的 return 值这样的解决方案,例如:
// 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);
我理解表达式的逻辑和 returning 一个值。但是有没有办法避免实现无意义的代码呢?
我自己找到了答案
Expression.Switch()
有过载 Switch(Type type, Expression switchValue, Expression defaultBody, MethodInfo comparison, IEnumerable<SwitchCase> cases);
当使用此重载并将 typeof(void)
传递给参数 type
时,它将接受它。
msdn: "All SwitchCase objects in a SwitchExpression object must have the same type, unless the SwitchExpression has the type void." 我没有在 SwitchExpression 类型 和包含 [= 的重载之间建立联系25=]类型 参数.
这将完成工作:
// 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);
我正在尝试实施 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);
这将引发此异常:All case bodies and the default body must have the same type.
我认为这指的是 Assign
returning 对于情况 1 是一个 int,对于情况 2 是一个字符串。 我怎么能return什么都没有?
如果我将 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);
引发异常:Default body must be supplied if case bodies are not System.Void.
所以我改了:最后一行改为
Expression.Switch(Expression.Constant("SwitchValue"), Expression.Constant(0), cases);
这将编译。但不是我要找的...
是否有另一种方法可以解决第一个解决方案,而无需对 return 相同类型实施额外的代码?
我不喜欢选择无意义的 return 值这样的解决方案,例如:
// 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);
我理解表达式的逻辑和 returning 一个值。但是有没有办法避免实现无意义的代码呢?
我自己找到了答案
Expression.Switch()
有过载 Switch(Type type, Expression switchValue, Expression defaultBody, MethodInfo comparison, IEnumerable<SwitchCase> cases);
当使用此重载并将 typeof(void)
传递给参数 type
时,它将接受它。
msdn: "All SwitchCase objects in a SwitchExpression object must have the same type, unless the SwitchExpression has the type void." 我没有在 SwitchExpression 类型 和包含 [= 的重载之间建立联系25=]类型 参数.
这将完成工作:
// 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);