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. Tomcat端口占用

    Tomcat端口占用问题 1.打开cmd,运行 netstat -aon!findstr "8080"查看该端口中运行的服务: 2.运行taskkill /pid PID /F 强 ...

  2. close与shutdown函数

    linux网络编程之socket(十):shutdown 与 close 函数的区别  http://blog.csdn.net/yijiu0711/article/details/17349169 ...

  3. 页面加载时执行JQ代码

    $(function () { //jq加载时执行的这里面是 $("#ss").append("<strong>这是新加的</strong>&qu ...

  4. 【leetcode】Next Permutation

    Next Permutation Implement next permutation, which rearranges numbers into the lexicographically nex ...

  5. C#值数值类型转换

    1.十进制转16进制 string result=number.ToString("X2"); >>0A //X2表示大写2位 2.字符串转数值类型 "); ...

  6. poj 1028

    http://poj.org/problem?id=1028 题意(水):做一个游览器的历史记录. back:后退到上一个页面,当上一个页面没有时,输出ignored. forward:向前一个页面, ...

  7. Java中使用Collections.sort()方法对数字和字符串泛型的LIst进行排序

    在List的排序中常用的是Collections.sort()方法,可以对String类型和Integer类型泛型的List集合进行排序. 首先演示sort()方法对Integer类型泛型的List排 ...

  8. 安装Django,运行django-admin.py startproject 工程名,后不出现指定的工程解决办法!!

       第一次写博客,,,,, 在看我这篇教程的前提是你应该已经正确装好python和Django了,好了,废话不说了,正题走你!你现在是不是很纠结自己运行django-admin.py startpr ...

  9. iOS-WKWebView携带cookie发送http请求,cookie失效

    发送请求代码: NSString *testUrl = @"http://10.22.122.7:8081/test2_action/view_index"; NSURL *url ...

  10. osgconv 批量转换

    @echo offfor /f "delims=" %%i in ('dir/b *.osg') do ( "osgconv.exe" "%%~ni. ...