using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks; namespace Unit
{
class T
{
public void Test()
{
Console.WriteLine("Test Class!");
}
}
class S
{
private List<int> ids = new List<int>();
public string Name { set; get; } public List<int> Ids { set { ids = value; } get { return ids; } }
}
class ET
{
public void Run()
{
//add
Add(, );//+
AddAssign(, );
Subtract(, );//-
SubtractAssign(,);
Multiply(,);
MultiplyAssign(,);
Divide(, );
DivideAssign(,);
Decrement();//i--;
Increment();//++
Modulo(); And(true, false); //&
AndAlso(true, false); //&&
Or(true, false);
OrElse(true, false); //||
LessThan(, );
Coalesce(, );
Coalesce(null, );
Is();
If();
IfElse();
Condition();
Switch();
Loop();
TypeAs();
Convert();
New();
NewArray();
NewArrayBounds();
ListInit();
ListBind();
}
/// <summary>
/// +
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
private void Add(int a,int b)
{
var left = Expression.Parameter(typeof(int));
var right = Expression.Parameter(typeof(int));
Expression exp = Expression.Add(left, right);
Expression<Func<int, int, int>> lambda = Expression.Lambda<Func<int, int, int>>(exp, new ParameterExpression[] { left, right });
var l = lambda.Compile();
var r = l(a, b);
Console.WriteLine(r);
} public static int Add2(int a,int b)
{
return ;
} /// <summary>
/// 利用方法+
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
private void AddAssign(int a, int b)
{
var left = Expression.Parameter(typeof(int));
var right = Expression.Parameter(typeof(int));
Expression exp = Expression.AddAssign(left, right, this.GetType().GetMethod("Add2"));
Expression<Func<int, int, int>> lambda = Expression.Lambda<Func<int, int, int>>(exp, new ParameterExpression[] { left, right });
var l = lambda.Compile();
var r = l(a, b);
Console.WriteLine(r);
}
/// <summary>
/// &
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
private void And(bool a, bool b)
{
var left = Expression.Parameter(typeof(bool));
var right = Expression.Parameter(typeof(bool));
Expression exp = Expression.And(left, right);
Expression<Func<bool, bool, bool>> lambda = Expression.Lambda<Func<bool, bool, bool>>(exp, new ParameterExpression[] { left, right });
var l = lambda.Compile();
var r = l(a, b);
Console.WriteLine(r);
}
/// <summary>
/// &&
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
private void AndAlso(bool a, bool b)
{
var left = Expression.Parameter(typeof(bool));
var right = Expression.Parameter(typeof(bool));
Expression exp = Expression.AndAlso(left, right);
Expression<Func<bool, bool, bool>> lambda = Expression.Lambda<Func<bool, bool, bool>>(exp, new ParameterExpression[] { left, right });
var l = lambda.Compile();
var r = l(a, b);
Console.WriteLine(r);
}
/// <summary>
/// |
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
private void Or(bool a, bool b)
{
var left = Expression.Parameter(typeof(bool));
var right = Expression.Parameter(typeof(bool));
Expression exp = Expression.Or(left, right);
Expression<Func<bool, bool, bool>> lambda = Expression.Lambda<Func<bool, bool, bool>>(exp, new ParameterExpression[] { left, right });
var l = lambda.Compile();
var r = l(a, b);
Console.WriteLine(r);
}
/// <summary>
/// ||
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
private void OrElse(bool a, bool b)
{
var left = Expression.Parameter(typeof(bool));
var right = Expression.Parameter(typeof(bool));
Expression exp = Expression.OrElse(left, right);
Expression<Func<bool, bool, bool>> lambda = Expression.Lambda<Func<bool, bool, bool>>(exp, new ParameterExpression[] { left, right });
var l = lambda.Compile();
var r = l(a, b);
Console.WriteLine(r);
} /// <summary>
/// -
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
private void Subtract(int a, int b)
{
var left = Expression.Parameter(typeof(int));
var right = Expression.Parameter(typeof(int));
Expression exp = Expression.Subtract(left, right);
Expression<Func<int, int, int>> lambda = Expression.Lambda<Func<int, int, int>>(exp, new ParameterExpression[] { left, right });
var l = lambda.Compile();
var r = l(a, b);
Console.WriteLine(r);
}
private void SubtractAssign(int a, int b)
{
var left = Expression.Parameter(typeof(int));
var right = Expression.Parameter(typeof(int));
Expression exp = Expression.SubtractAssign(left, right, this.GetType().GetMethod("Add2"));
Expression<Func<int, int, int>> lambda = Expression.Lambda<Func<int, int, int>>(exp, new ParameterExpression[] { left, right });
var l = lambda.Compile();
var r = l(a, b);
Console.WriteLine(r);
} /// <summary>
/// *
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
private void Multiply(int a, int b)
{
var left = Expression.Parameter(typeof(int));
var right = Expression.Parameter(typeof(int));
Expression exp = Expression.Multiply(left, right);
Expression<Func<int, int, int>> lambda = Expression.Lambda<Func<int, int, int>>(exp, new ParameterExpression[] { left, right });
var l = lambda.Compile();
var r = l(a, b);
Console.WriteLine(r);
}
private void MultiplyAssign(int a, int b)
{
var left = Expression.Parameter(typeof(int));
var right = Expression.Parameter(typeof(int));
Expression exp = Expression.MultiplyAssign(left, right, this.GetType().GetMethod("Add2"));
Expression<Func<int, int, int>> lambda = Expression.Lambda<Func<int, int, int>>(exp, new ParameterExpression[] { left, right });
var l = lambda.Compile();
var r = l(a, b);
Console.WriteLine(r);
}
/// <summary>
/// /
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
private void Divide(int a, int b)
{
var left = Expression.Parameter(typeof(int));
var right = Expression.Parameter(typeof(int));
Expression exp = Expression.Divide(left, right);
Expression<Func<int, int, int>> lambda = Expression.Lambda<Func<int, int, int>>(exp, new ParameterExpression[] { left, right });
var l = lambda.Compile();
var r = l(a, b);
Console.WriteLine(r);
}
/// <summary>
/// a/=b
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
private void DivideAssign(int a, int b)
{
var left = Expression.Parameter(typeof(int));
var right = Expression.Parameter(typeof(int));
Expression exp = Expression.DivideAssign(left, right, this.GetType().GetMethod("Add2"));
Expression<Func<int, int, int>> lambda = Expression.Lambda<Func<int, int, int>>(exp, new ParameterExpression[] { left, right });
var l = lambda.Compile();
var r = l(a, b);
Console.WriteLine(r);
}
/// <summary>
/// --
/// </summary>
/// <param name="a"></param>
private void Decrement(int a)
{
var left = Expression.Parameter(typeof(int));
Expression exp = Expression.Decrement(left);
Expression<Func<int, int>> lambda = Expression.Lambda<Func<int, int>>(exp, new ParameterExpression[] { left });
var l = lambda.Compile();
var r = l(a);
Console.WriteLine(r);
}
/// <summary>
/// ++
/// </summary>
/// <param name="a"></param>
private void Increment(int a)
{
var left = Expression.Parameter(typeof(int));
Expression exp = Expression.Increment(left);
Expression<Func<int, int>> lambda = Expression.Lambda<Func<int, int>>(exp, new ParameterExpression[] { left });
var l = lambda.Compile();
var r = l(a);
Console.WriteLine(r);
}
/// <summary>
/// >
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
private void LessThan(int a, int b)
{
var left = Expression.Parameter(typeof(int));
var right = Expression.Parameter(typeof(int));
Expression exp = Expression.LessThan(left, right);
Expression<Func<int, int, bool>> lambda = Expression.Lambda<Func<int, int, bool>>(exp, new ParameterExpression[] { left, right });
var l = lambda.Compile();
var r = l(a,b);
Console.WriteLine(r);
}
/// <summary>
/// ??
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
private void Coalesce(int? a, int b)
{
var left = Expression.Parameter(typeof(int?));
var right = Expression.Parameter(typeof(int)); var right2 = Expression.Assign(right, Expression.Default(typeof(int)));
Expression exp = Expression.Coalesce(left, right2);
Expression<Func<int?, int, int>> lambda = Expression.Lambda<Func<int?, int, int>>(exp, new ParameterExpression[] { left, right });
var l = lambda.Compile();
var r = l(a, b);
Console.WriteLine(r);
} public static bool IsFalse(bool flag){
return false;
}
/// <summary>
/// is
/// </summary>
private void Is()
{
var v1 = Expression.Parameter(typeof(bool)); var v11 = Expression.Assign(v1,Expression.Constant(true));
var v2 = Expression.IsFalse(v1, this.GetType().GetMethod("IsFalse"));
Expression exp = Expression.IsFalse(v2);
var v3=Expression.TypeIs(Expression.Constant(new List<int>()),typeof(IList<int>));
Expression<Func<bool,bool>> lambda = Expression.Lambda<Func<bool,bool>>(exp, new ParameterExpression[] {v1 });
var l = lambda.Compile();
var r = l(true);
Console.WriteLine(r);
Expression<Func<bool>> lambda2 = Expression.Lambda<Func<bool>>(v3);
var l2 = lambda2.Compile();
Console.WriteLine(l2());
}
/// <summary>
/// if
/// </summary>
private void If()
{
var v1 = Expression.Variable(typeof(int),"a");
var v11 = Expression.Assign(v1, Expression.Constant());
var v2 = Expression.GreaterThan(v11, Expression.Constant());
Expression exp = Expression.IfThen(v2,Expression.Call(typeof(Console).GetMethod("WriteLine",new Type[]{typeof(int)}),v1));
Expression exp2 = Expression.Block(new[]{v1},exp);
Expression<Action> lambda = Expression.Lambda<Action>(exp2, new ParameterExpression[] { });
var l = lambda.Compile();
l();
} /// <summary>
/// if else
/// </summary>
private void IfElse()
{
var v1 = Expression.Variable(typeof(int), "a");
var v11 = Expression.Assign(v1, Expression.Constant());
var v2 = Expression.GreaterThan(v11, Expression.Constant());
Expression exp = Expression.IfThenElse(v2, Expression.Call(typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), Expression.Constant("True")), Expression.Call(typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), Expression.Constant("False")));
Expression exp2 = Expression.Block(new[] { v1 }, exp);
Expression<Action> lambda = Expression.Lambda<Action>(exp2, new ParameterExpression[] { });
var l = lambda.Compile();
l();
} /// <summary>
/// trueTest?ifTrue:ifFalse;
/// </summary>
private void Condition()
{
var v1 = Expression.Variable(typeof(int), "a");
var v11 = Expression.Assign(v1, Expression.Constant());
var v2 = Expression.GreaterThan(v1, Expression.Constant());
Expression exp = Expression.Condition(v2, Expression.Call(typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), Expression.Constant("True")), Expression.Call(typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), Expression.Constant("False")));
Expression exp2 = Expression.Block(new[] { v1 },v11, exp);
Expression<Action> lambda = Expression.Lambda<Action>(exp2, new ParameterExpression[] { });
var l = lambda.Compile();
l();
}
/// <summary>
/// switch
/// </summary>
/// <param name="i"></param>
private void Switch(int i)
{
var par = Expression.Parameter(typeof(int));
var target = Expression.Label(typeof(int),"a");
var exp = Expression.Switch(par, Expression.Break(target, Expression.Constant(-)), Expression.SwitchCase(Expression.Break(target, par), Expression.Constant()));
var lbl = Expression.Label(target, Expression.Constant());
var block = Expression.Block(exp,lbl);
Expression<Func<int, int>> lambda = Expression.Lambda<Func<int, int>>(block, new ParameterExpression[] { par });
var l = lambda.Compile();
Console.WriteLine(l(i));
}
/// <summary>
/// while(true){}
/// </summary>
/// <param name="i"></param>
private void Loop(int i)
{
var par = Expression.Parameter(typeof(int));
var ret = Expression.Variable(typeof(int), "ret");
var v11 = Expression.Assign(ret, Expression.Constant());
var lbl = Expression.Label(typeof(int));
var lbl2 = Expression.Label(lbl,Expression.Constant());
var block1 = Expression.Block(Expression.AddAssign(ret,par),Expression.SubtractAssign(par,Expression.Constant()));
var condition = Expression.IfThenElse(Expression.GreaterThan(par, Expression.Constant()),block1, Expression.Break(lbl, ret));
var block2 = Expression.Block(new[] { ret }, v11, Expression.Loop(condition), lbl2);// Expression.Block(new[] { ret }, v11, Expression.Loop(condition,lbl)) 可以达到一样的效果
Expression<Func<int, int>> lambda = Expression.Lambda<Func<int, int>>(block2, new ParameterExpression[] { par });
var l = lambda.Compile();
Console.WriteLine(l(i));
} /// <summary>
/// as
/// </summary>
private void TypeAs()
{
Expression s = Expression.Constant();
var t=Expression.TypeAs(s, typeof(object));
var call = Expression.Call(typeof(Console).GetMethod("WriteLine", new[] { typeof(int) }), Expression.Unbox(t, typeof(int)));
Expression<Action> lambda = Expression.Lambda<Action>(call);
var l=lambda.Compile();
l();
} /// <summary>
/// 强转 class定义TypeConverter,TypeConverterAttribute
/// </summary>
private void Convert()
{
Expression s = Expression.Constant(7.5);
var t = Expression.Convert(s, typeof(int));
var call = Expression.Call(typeof(Console).GetMethod("WriteLine", new[] { typeof(int) }),t);
Expression<Action> lambda = Expression.Lambda<Action>(call);
var l = lambda.Compile();
l();
} /// <summary>
/// %
/// </summary>
private void Modulo()
{
Expression s1 = Expression.Constant();
Expression s2 = Expression.Constant();
var t = Expression.Modulo(s1,s2); Expression<Func<int>> lambda = Expression.Lambda<Func<int>>(t);
var l = lambda.Compile();
Console.WriteLine(l());
} /// <summary>
/// new
/// </summary>
private void New()
{
Expression s = Expression.Constant();
var t = Expression.New(typeof(T));
var call = Expression.Call(t, typeof(T).GetMethod("Test"));
Expression<Action> lambda = Expression.Lambda<Action>(call);
var l = lambda.Compile();
l();
} /// <summary>
/// 初始化数组对象new int[]{7}
/// </summary>
private void NewArray()
{
Expression s = Expression.Constant();
var t = Expression.NewArrayInit(typeof(int),s);
var call = Expression.Call(t, typeof(int[]).GetProperty("Length").GetGetMethod());
Expression<Func<int>> lambda = Expression.Lambda<Func<int>>(call);
var l = lambda.Compile();
Console.WriteLine(l());
} /// <summary>
/// new int[7]
/// </summary>
private void NewArrayBounds()
{
Expression s = Expression.Constant();
var t = Expression.NewArrayBounds(typeof(int), s);
var call = Expression.Call(t, typeof(int[]).GetProperty("Length").GetGetMethod());
Expression<Func<int>> lambda = Expression.Lambda<Func<int>>(call);
var l = lambda.Compile();
Console.WriteLine(l());
}
/// <summary>
/// new List<int>(){5}
/// </summary>
private void ListInit()
{
Expression s = Expression.Constant();
var lst = Expression.New(typeof(List<int>));
var t = Expression.ListInit(lst,s);
var call = Expression.Call(t, typeof(List<int>).GetProperty("Count").GetGetMethod());
Expression<Func<int>> lambda = Expression.Lambda<Func<int>>(call);
var l = lambda.Compile();
Console.WriteLine(l());
}
/// <summary>
/// var s=new S();s.Ids.Add(5);s.Ids.Add(7);
/// </summary>
private void ListBind()
{
Type s = typeof(S);
var n=Expression.New(s);
var ids=s.GetProperty("Ids");
var l=typeof(List<int>);
var n2 = Expression.New(l);
var e1 = Expression.ElementInit(l.GetMethod("Add"), Expression.Constant());
var e2 = Expression.ElementInit(l.GetMethod("Add"), Expression.Constant());
var lst = Expression.ListBind(ids,e1,e2);
var init=Expression.MemberInit(n,lst);
Expression<Func<S>> lambda = Expression.Lambda<Func<S>>(init);
var ln = lambda.Compile();
var o = ln();
Console.WriteLine(o);
}
/// <summary>
/// L_0000: ldarg.1
/// L_0001: call void [mscorlib]System.Console::WriteLine(int32)
/// L_0006: ldarg.s j2
/// L_0008: call void [mscorlib]System.Console::WriteLine(int32)
/// L_000d: ret
/// </summary>
private void TestArgs(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, int a1, int b1, int c1, int d1, int e1, int f1, int g1, int h1, int i1, int j1, int a2, int b2, int c2, int d2, int e2, int f2, int g2, int h2, int i2, int j2)
{ Console.WriteLine(a);
Console.WriteLine(j2);
}
}
}

Expression基础体验的更多相关文章

  1. Java 8 新特性之 Stream 流基础体验

    Java 8 新特性之 Stream 流基础体验 package com.company; import java.util.ArrayList; import java.util.List; imp ...

  2. 零基础学习python 你该怎么做

    本人文科生,回顾自己近 2 年的Python 自学经历,有一些学习心得和避坑经验分享给大家,让大家在学习 Python 的过程中少走一些弯路!减少遇到不必要的学习困难! 首先,最开始最大的困难应该就是 ...

  3. CI Weekly #4 | 不同规模的团队,如何做好持续集成?

    CI Weekly 围绕『 软件工程效率提升』 进行一系列技术内容分享,包括国内外持续集成.持续交付,持续部署.自动化测试. DevOps 等实践教程.工具与资源,以及一些工程师文化相关的程序员 Ti ...

  4. MVC验证01-基础、远程验证

    本文体验MVC服务端和客户端验证.主要涉及:※ 基础验证※ 远程验证1个或多个属性及注意点 基础体验 创建MVC4的Internet项目,本身包含了基本的Model,Views,Controller. ...

  5. H5 App设计者需要注意的21条禁忌

    我们通常在做H5 APP设计的过程中,遇到很多看似很小,且很容易被忽略的问题,正是这些小问题,一次次的撩拨用户的耐心,让用户对你的APP心生怨念.现在WeX5君呕血为大家整理出H5 APP设计的21条 ...

  6. android 类似QQ底部输入框弹出键盘和面板冲突 布局闪动处理方案(转)

    先看下效果 差不多就是解决这种冲突,布局闪动的 作者的githup :https://github.com/Jacksgong/JKeyboardPanelSwitch Android键盘面板冲突 布 ...

  7. 01Vue数据双向绑定

    Vue作为前端MV*架构,Vue.js (读音 /vjuː/,类似于 view) 是一套构建用户界面的渐进式框架.与其他重量级框架不同的是,Vue 采用自底向上增量开发的设计. Vue 的核心库只关注 ...

  8. QQ音乐的动效歌词是如何实践的?

    本文由云+社区发表 作者:QQ音乐技术团队 一. 背景 1. 现状 歌词浏览已经成为音乐app的标配,展示和动画效果也基本上大同小异,主要是单行的逐字染色的卡拉OK效果和多行的滚动效果.当然,我们也不 ...

  9. Java 11 正式发布,这 8 个逆天新特性教你写出更牛逼的代码

    美国时间 09 月 25 日,Oralce 正式发布了 Java 11,这是据 Java 8 以后支持的首个长期版本. 为什么说是长期版本,看下面的官方发布的支持路线图表. 可以看出 Java 8 扩 ...

随机推荐

  1. Linux 故障问题处理

    一.   Debian   网卡问题 原因: 网卡提示 Device Not Managed 处理方法: . 编辑/etc/NetworkManager/NetworkManager.conf: 将其 ...

  2. HTML5新增常用属性

    一. 代码名称语义化的好处 1.能让搜索引擎更好的收录 2.对于特殊设备如盲人设备好解析 二.article和section的区别 article(文章):独立且能被外部引用 section(章节.段 ...

  3. maven 安装m2e 报错

    Eclipse安装maven插件m2e m2e - http://m2eclipse.sonatype.org/sites/m2e m2e-extras - http://m2eclipse.sona ...

  4. 用 webpack 创建 vue 项目

    1.安装支持webpack 包 npm i -D  webpack webpack-cli  aspnet-webpack webpack-dev-middleware  webpack-hot-mi ...

  5. Linux监控工具讲解

    本文主要记录一下 Linux系统上一些常用的系统监控工具,非常好用.正所谓磨刀不误砍柴工,花点时间总结一下是值得的! 本文内容脑图如下: top 命令 top 命令我想大家都挺熟悉吧!Linux 下的 ...

  6. oracle 事务 与 提交

    Oracle事务 一般事务(DML)即数据修改(增.删.改)的事务事务会将所有在事务中被修改的数据行加上锁(行级锁),来阻止其它人(会话)同时对这些数据的修改操作.当事务被提交或回滚后,这些数据才会被 ...

  7. 后端必备的Linux知识

    目录 从认识操作系统开始 操作系统简介 操作系统简单分类 初探Linux Linux简介 Linux诞生简介 Linux的分类 Linux文件系统概览 Linux文件系统简介 文件类型与目录结构 Li ...

  8. pinpoint初始化hbase脚本报错

    今天在部署pinpoint的时候,执行创建表语句的脚本,报表已经存在的错误,但是那个hbase数据目录是刚创建的,表肯定是不存在的 <property> <name>hbase ...

  9. android开发_文本按钮 与 输入框

    1 TextView:    属性与值 android:text="文本" android:textSize="20sp"              //sp为 ...

  10. 在Linux(Debian)环境下搭建并运行GPU

    首先通过以下命令查看是否GPU驱动成功: 注意:需要在bash终端输入 import tensorflow as tf hello = tf.constant('Hello, TensorFlow!' ...