zoukankan      html  css  js  c++  java
  • C# Expression表达式笔记

    整理了一下表达式树的一些东西,入门足够了

    先从ConstantExpression 开始一步一步的来吧  它表示具有常量值的表达式

    我们选建一个控制台应用程序

    ConstantExpression _constExp = Expression.Constant("aaa",typeof(string));//一个常量
    //Console.Writeline("aaa");
    MethodCallExpression _methodCallexp=Expression.Call(typeof(Console).GetMethod("WriteLine",new Type[]{typeof(string)}),_constExp);
    Expression<Action> consoleLambdaExp = Expression.Lambda<Action>(_methodCallexp);
    consoleLambdaExp.Compile()();
     
    Console.ReadLine();

    下边的MethodCallExpression你也许不知道是什么回事,不要急我下边会详细讲的,这相当于

    Console.WriteLine("aaa");  输出一个常量,看一下结果

    如果想自己输入一个值输出呢,那就用ParameterExpression 它表示一个参数表达式,我们只要把上边的代码做一下小改动就行

    ParameterExpression _parameExp = Expression.Parameter(typeof(string), "MyParameter");
     
    MethodCallExpression _methodCallexpP = Expression.Call(typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), _parameExp);
    Expression<Action<string>> _consStringExp = Expression.Lambda<Action<string>>(_methodCallexpP, _parameExp);
    _consStringExp.Compile()("Hello!!");

    参数parameExp就是一个string类型的变量我们让它输出一个Hello!!

    有点感觉了吧,慢慢来好玩的还在后边,现在我们就说一下MethodCallExpression它可以调用静态方法和实例方法,我们上边的代码就是调用 的静态方法

    ,我先讲一下调用静态方法,再讲调用实例方法。

    我们建一个返回string的静态方法,传入一个object类型的值

    public static string ConsStr(object str)
    {
        string _str = str + "aa";
        Console.WriteLine(_str);
        return _str;
    }

    看一下我们是怎么调用自己的静态方法的

    ParameterExpression _paraObj = Expression.Parameter(typeof(object), "objPara");
    MethodCallExpression _MyStateMethod = Expression.Call(typeof(Program).GetMethod("ConsStr", new Type[] { typeof(object) }), _paraObj);
    Expression<Func<object, string>> _meyLambdaState = Expression.Lambda<Func<object, string>>(_MyStateMethod, _paraObj);
    string s_tr = _meyLambdaState.Compile()("ni Hao");
    Console.WriteLine("返回值: " + s_tr);

      new Type[] { typeof(object) } 就是我们的方法里的参数类型,后边的paraObj是相当于参数值了,如果 是多参数就在 Type[],和后边再加上相应 的类型和参数就行

    静态方法你有些了解了,下面讲一下调用实例方法

    我们写一个非静态方法

    public string ConsStr2(object str)
    {
        string _str = str + "aa";
        Console.WriteLine(_str);
        return _str;
    }

    调用的时候只要把上边的代码改动一点就ok Expression.Call为我们提供了我们想要的重载

    Program _pg = new Program();
    ParameterExpression _paraObj2 = Expression.Parameter(typeof(object), "objPara");
    MethodCallExpression _MyStateMethod2 = Expression.Call(Expression.Constant(_pg), typeof(Program).GetMethod("ConsStr2"), _paraObj2);
    Expression<Func<object, string>> _meyLambdaState2 = Expression.Lambda<Func<object, string>>(_MyStateMethod2, _paraObj2);
    string s_tr2 = _meyLambdaState.Compile()("you shi ni ");
    Console.WriteLine("返回值: " + s_tr2);

       简单吧。

    再下来我们讲什么呢,也许你猜到了UnaryExpression一元运算符表达式和 BinaryExpression  二元运算符表达式

    我们先看一个这两个表达式的简单例子后,我们再做一个复杂的例子

    UnaryExpression我们做一个5--的表达式

    ConstantExpression _consNum = Expression.Constant(5, typeof(int));
    UnaryExpression _unaryPlus = Expression.Decrement(_consNum);
    Expression<Func<int>> _unaryLam = Expression.Lambda<Func<int>>(_unaryPlus);
    Console.WriteLine(_unaryLam.Compile()());

    BinaryExpression  我们做一个a+b的例子 

    ParameterExpression _ParaA = Expression.Parameter(typeof(int), "a");
    ParameterExpression _ParaB = Expression.Parameter(typeof(int), "b");
    BinaryExpression _BinaAdd = Expression.Add(_ParaA, _ParaB);
    Expression<Func<int, int, int>> _MyBinaryAddLamb = Expression.Lambda<Func<int, int, int>>(_BinaAdd, new ParameterExpression[] { _ParaA, _ParaB });
    Console.WriteLine("表达式:  "+ _MyBinaryAddLamb);
    Console.WriteLine(_MyBinaryAddLamb.Compile()(3, 6));

      不难吧,

    我们做一把两个表达式放一起做一个例子吧 (a+b)*(--c)

    ParameterExpression _ParaA = Expression.Parameter(typeof(int), "a");
    ParameterExpression _ParaB = Expression.Parameter(typeof(int), "b");
    BinaryExpression _BinaAdd = Expression.Add(_ParaA, _ParaB);  //a+b
     
    ParameterExpression _paraC = Expression.Parameter(typeof(int), "c");
    UnaryExpression _paraDecr = Expression.Decrement(_paraC);    //(a+b)*(--c)
    BinaryExpression _binaMultiply = Expression.Multiply(_BinaAdd, _paraDecr);
    Expression<Func<int, int, int, int>> _MyBinaryLamb = Expression.Lambda<Func<int, int, int, int>>(_binaMultiply, new ParameterExpression[] { _ParaA, _ParaB, _paraC });
    Console.WriteLine("表达式:  "+ _MyBinaryLamb);
    Console.WriteLine(_MyBinaryLamb.Compile()(3, 6, 5));

      

    今天就讲到这

  • 相关阅读:
    CF1359D Yet Another Yet Another Task
    【数据结构】fhq_treap
    AtCoder Beginner Contest 182 题解
    UVA11992 Fast Matrix Operations
    双指针例题
    python使用国内镜像库
    APP元素定位工具之——Weditor
    安卓ADB的常见命令的使用
    函数进阶之迭代器,递归
    函数基础之对象,嵌套,名称空间和作用域
  • 原文地址:https://www.cnblogs.com/tiancai/p/5063307.html
Copyright © 2011-2022 走看看