Expression Tree 第一个简单的例子。

 [TestMethod]
        public void GodTest()
        {
            Expression<Func<int, int, int>> expression = (a, b) => a + b;

            var func = expression.Compile();
            var r = func(1, 2);
            Assert.AreEqual(3, r);

            var s = Expression.Variable(typeof(string), "s");
            var assignExpression = Expression.Assign(s, Expression.Constant("Hello World!"));

            var callExpression = Expression.Call(null, typeof(Console).GetMethod("WriteLine", new[] { typeof(object) }), s);

            var writeLineExpression = Expression.Lambda<Action>(
                Expression.Block(
                new[] { s }, assignExpression, callExpression));

            var writeLine = writeLineExpression.Compile();
            writeLine();
        }

一个简单的例子:

  class Program
    {
        static void Main(string[] args)
        {
            Expression e1 = Expression.Constant(1);
            Expression e2 = Expression.Constant(2);
            Expression add = Expression.Add(e1, e2);

            var compiled = Expression.Lambda<Func<int>>(add).Compile();
            Console.WriteLine(compiled());
        }
    }

.NET Expression Tree的更多相关文章

  1. Expression Tree Basics 表达式树原理

    variable point to code variable expression tree data structure lamda expression anonymous function 原 ...

  2. Expression Tree 扩展MVC中的 HtmlHelper 和 UrlHelper

    表达式树是LINQ To everything 的基础,同时各种类库的Fluent API也 大量使用了Expression Tree.还记得我在不懂expression tree时,各种眼花缭乱的A ...

  3. 使用Expression Tree构建动态LINQ查询

    这篇文章介绍一个有意思的话题,也是经常被人问到的:如何构建动态LINQ查询?所谓动态,主要的意思在于查询的条件可以随机组合,动态添加,而不是固定的写法.这个在很多系统开发过程中是非常有用的. 我这里给 ...

  4. Reflection和Expression Tree解析泛型集合快速定制特殊格式的Json

    很多项目都会用到Json,而且大部分的Json都是格式固定,功能强大,转换简单等,标准的key,value集合字符串:直接JsonConvert.SerializeObject(List<T&g ...

  5. Evaluation of Expression Tree

    Evaluation of Expression Tree Given a simple expression tree, consisting of basic binary operators i ...

  6. 转载Expression Tree揭秘

    概述 在.NET Framework 3.5中提供了LINQ 支持后,LINQ就以其强大而优雅的编程方式赢得了开发人员的喜爱,而各种LINQ Provider更是满天飞,如LINQ to NHiber ...

  7. 深入学习C#匿名函数、委托、Lambda表达式、表达式树类型——Expression tree types

    匿名函数 匿名函数(Anonymous Function)是表示“内联”方法定义的表达式.匿名函数本身及其内部没有值或者类型,但是可以转换为兼容的委托或者表达式树类型(了解详情).匿名函数转换的计算取 ...

  8. Expression Tree Build

    The structure of Expression Tree is a binary tree to evaluate certain expressions.All leaves of the ...

  9. 表达式树(Expression Tree)

    饮水思源 本文并非原创而是下面网址的一个学习笔记 https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/e ...

随机推荐

  1. Pairing heap

    其实挺好写的...(大雾) 理性愉悦大法好... #include<cstdio> #include<algorithm> namespace utils{ template& ...

  2. Buffer和Cache的区别

  3. 【GoLang】GoLang for 中有多个循环变量怎么处理?

    代码示例: sum := , ; i <= && j <= ; i, j = i+, j- { t.Log("i: ", i) t.Log(" ...

  4. 【Java】Java 深入探讨 单例模式的实现

    在GoF的23种设计模式中,单例模式是比较简单的一种.然而,有时候越是简单的东西越容易出现问题.下面就单例设计模式详细的探讨一下.   所谓单例模式,简单来说,就是在整个应用中保证只有一个类的实例存在 ...

  5. shiro学习中报错解决方法

    [1] 最近在学习shiro,在学习过程中出现了一个问题,报错如下: org.apache.shiro.UnavailableSecurityManagerException: No Security ...

  6. poj 1035

    http://poj.org/problem?id=1035 poj的一道字符串的水题,不难,但就是细节问题我也wa了几次 题意就是给你一个字典,再给你一些字符,首先如果字典中有这个字符串,则直接输出 ...

  7. ios 在ios9中 NSNotificationCenter addObserver 不会影响对象释放

    如题,ios9上,  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test) name:@&qu ...

  8. POJ 2965 The Pilots Brothers' refrigerator

    题目链接 题意:一个冰箱上有4*4共16个开关,改变任意一个开关的状态(即开变成关,关变成开)时,此开关的同一行.同一列所有的开关都会自动改变状态.要想打开冰箱,要所有开关全部打开才行. 输入:一个4 ...

  9. Qt 获取Mac地址

    QList<QNetworkInterface> list = QNetworkInterface::allInterfaces(); foreach(QNetworkInterface ...

  10. 查看base64编码图片

    1.确认编码纯净(没有编码参数) 2.在头部加上 data:image/jpeg;base64, 3.放到浏览器查看