【问题标题】:Expression.SwitchCase matching issueExpression.SwitchCase 匹配问题
【发布时间】:2018-05-08 19:14:20
【问题描述】:

这是从MSDN复制的示例。

ConstantExpression switchValue = Expression.Constant(3);

// This expression represents a switch statement 
// that has a default case.
SwitchExpression switchExpr =
    Expression.Switch(
        switchValue,
        Expression.Call(
                    null,
                    typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
                    Expression.Constant("Default")
                ),
        new SwitchCase[] {
            Expression.SwitchCase(
                Expression.Call(
                    null,
                    typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
                    Expression.Constant("First")
                ),
                Expression.Constant(1)
            ),
            Expression.SwitchCase(
                Expression.Call(
                    null,
                    typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
                    Expression.Constant("Second")
                ),
                Expression.Constant(2)
            )
        }
    );

// The following statement first creates an expression tree,
// then compiles it, and then runs it.
Expression.Lambda<Action>(switchExpr).Compile()();
//Default

它工作正常,将“默认”打印到控制台。

我的问题是如何使表达式符合下一种情况(“第一”)。这是我尝试过的:

ParameterExpression pe = Expression.Parameter(typeof(int));
Expression.Lambda<Action<int>>(switchExpr,pe).Compile()(1);
//
ParameterExpression peo = Expression.Parameter(typeof(object));
object o = 1;
Expression.Lambda<Action<object>>(switchExpr, peo).Compile()(o);

他们都没有将“First”打印到控制台。怎么了?谢谢。

更新:

我认为MSDN的代码不是一个完美的例子,为什么要打开一个常量?

【问题讨论】:

    标签: c# lambda expression


    【解决方案1】:

    在本页的示例中,您可以看到如何打印“第二”值:

    https://msdn.microsoft.com/en-us/library/system.linq.expressions.switchcase(v=vs.110).aspx

    您的示例根本没有使用该值,而是使用示例开头的固定 3:

    ConstantExpression switchValue = Expression.Constant(3);
    

    希望对你有帮助。

    【讨论】:

    • 你是对的。我的错。无论如何要在运行时传递值?
    【解决方案2】:

    感谢@Ignacio 解决问题。

    这是我想出的代码。

    完整代码

    ParameterExpression pe = Expression.Parameter(typeof(int));
    
    SwitchExpression switchExpr =
        Expression.Switch(
            pe,
            Expression.Call(
                        null,
                        typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
                        Expression.Constant("Default")
                    ),
            new SwitchCase[] {
                Expression.SwitchCase(
                    Expression.Call(
                        null,
                        typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
                        Expression.Constant("First")
                    ),
                    Expression.Constant(1)
                ),
                Expression.SwitchCase(
                    Expression.Call(
                        null,
                        typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
                        Expression.Constant("Second")
                    ),
                    Expression.Constant(2)
                )
            }
        );
    
    var action = Expression.Lambda<Action<int>>(switchExpr,pe).Compile();
    action(1);
    action(2);
    action(3);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-02
      • 1970-01-01
      • 2015-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多