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. Python socket的客户端

    做一个socket客户端1.声明一个实例2.绑定端口号和地址3.循环发送和接收响应其中要注意粘包的产生,为了防止粘包的产生,应该在服务器端先测出要发送信息的大小,然后发送响应至客户端,等到服务器上一条 ...

  2. Spring中@Async

    在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的:但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在spring 3.x之后, ...

  3. Replica Set + sharding搭建mongodb集群

    先上几个用到的yaml文件,以后有时间了再更新,我是借鉴的https://blog.csdn.net/zeroctu/article/details/71082168,本人按照上面的搭建,是可行的,不 ...

  4. 日常开发工作常用linux命令

    :wq 保存退出 :q! 强制退出 vi 查看 vim 编辑 rpm -qa|grep jdk 命令查看当前的jdk情况 yum -y remove java java-1.7.0-openjdk* ...

  5. svn基础了解

    Apache Subversion 通常被缩写成 SVN,是一个开放源代码的版本控制系统 Subversion(SVN) 是一个开源的版本控制系統, 也就是说 Subversion 管理着随时间改变的 ...

  6. mysql在Windows下使用mysqldump命令手动备份数据库和自动备份数据库

    手动备份: cmd控制台: 先进入mysql所在的bin目录下,如:cd C:\Program Files\MySQL\MySQL Server 5.5\bin mysqldump -u root - ...

  7. 在java中实现数据导入excel表格中

    1.首先前端代码如下: 一个导出按钮:<input id="export" class="btn btn-primary" type="butt ...

  8. android 模拟器 访问 localhost IIs Express 400错误

    如果用安卓模拟器调试本机的网站,比如 localhost:63586,是访问不到的,返回结果是 400 错误.原因是模拟器上的localhost指向的默认是模拟器自己. 解决方法是:(1) 把安卓模拟 ...

  9. ps top 命令

    pstree :显示进程树 ps: a:查看和终端有关的进程 u:显示进程是哪个用户启动的 x:和终端无关 ps aux |head 进程的分类: 和终端有关 和终端无关 进程状态: D:不可中断睡眠 ...

  10. XUnit测试框架-Python unittest

    选择 语言选择 本次个人作业我选择的语言是Python,了解学习Python有一段时间了但是一直没有练习,所以这次玩蛇,使用的版本是Python3.6. 开发工具选择 我选择的IDE是Pycharm, ...