美文网首页
C# 表达式树 (15)

C# 表达式树 (15)

作者: wwmin_ | 来源:发表于2021-02-07 21:33 被阅读0次

    系列文章: C# 表达式树及其应用 (Expression 类)

    本节继续讲解Expression相关的API, 力争把相关api 都使用简短demo说明一下

    • Return 创建一个表示 return 语句的 GotoExpression。
    //Return    创建一个表示 return 语句的 GotoExpression。
        void ReturnSample()
        {
            LabelTarget returnTarget = Expression.Label();
            BlockExpression blockExpr = Expression.Block(
                Expression.Call(typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) })!, Expression.Constant("Return")),
                Expression.Return(returnTarget),
                Expression.Call(typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) })!, Expression.Constant("Other Work")),
                Expression.Label(returnTarget)
            );
            Expression.Lambda<Action>(blockExpr).Compile()();//Return
        }
        ReturnSample();
    
        //RightShift    创建一个表示按位右移运算的 BinaryExpression。同 LeftShift
    
        //RightShiftAssign  创建一个表示按位右移赋值运算的 BinaryExpression。 同LeftShiftAssign
    
        //RuntimeVariables  创建 RuntimeVariablesExpression的实例。//TODO
    
        //Subtract  创建一个表示不进行溢出检查的算术减法运算的 BinaryExpression。 同Add
    
        //SubtractAssign  创建一个表示不进行溢出检查的减法赋值运算的 BinaryExpression。  同AddAssign
    
    • Switch 创建一个表示 SwitchExpression 语句的 switch。
        //Switch    创建一个表示 SwitchExpression 语句的 switch。
        void SwitchSample()
        {
            ConstantExpression switchValue = Expression.Constant(2);
            ParameterExpression pamaramNum = Expression.Parameter(typeof(int), "num");
            SwitchExpression switchExpr = Expression.Switch(
                pamaramNum,
                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)
                    )
                }
            );
            LambdaExpression lambda = Expression.Lambda(switchExpr, new List<ParameterExpression>() { pamaramNum });
            lambda.ToString().Dump("Switch");//() => switch (2) { ... }
            lambda.Compile().DynamicInvoke(1);
        }
        SwitchSample();
    
        //SwitchCase   创建要在 SwitchCase 对象中使用的 SwitchExpression 对象。  同Switch
    
    • SymbolDocument 创建 SymbolDocumentInfo的实例。
        //SymbolDocument  创建 SymbolDocumentInfo的实例。
        void SymbolDocumentSample()
        {
            var symbol = Expression.SymbolDocument("sss");
            symbol.ToString().Dump("Symbolocument");//System.Linq.Expressions.SymbolDocumentInfo
        }
        SymbolDocumentSample();
    

    本文作者:wwmin
    微信公众号: DotNet技术说
    本文链接:https://www.jianshu.com/p/6228bdda0655
    关于博主:评论和私信会在第一时间回复。或者[直接私信]我。
    版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
    声援博主:如果您觉得文章对您有帮助,关注点赞, 您的鼓励是博主的最大动力!

    相关文章

      网友评论

          本文标题:C# 表达式树 (15)

          本文链接:https://www.haomeiwen.com/subject/sddtxltx.html