using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Linq.Expressions; namespace INPEXOne.LearnCS
{
class RefletLambdaDelegate
{
static object[] parameters = new object[] { , };
public delegate int addDelegate(int a, int b); public int add(int a ,int b)
{
return a + b;
} public static void Main()
{
RefletLambdaDelegate rld=new RefletLambdaDelegate();
//利用反射调用add方法.
MethodInfo add = typeof(RefletLambdaDelegate).GetMethod("add");
Console.WriteLine("The add result from using the reflection is : {0}",add.Invoke(rld, parameters)); //利用Lambda表达式树调用add方法.
Expression<Func<int, int, int>> extree = (a,b) => rld.add(a, b);
Func<int, int, int> delinstance = extree.Compile();
int result = delinstance(int.Parse(parameters[].ToString()), int.Parse(parameters[].ToString()));
Console.WriteLine("The add result from using the LambdaTree is : {0}",result); //利用Lambda表达式调用add方法.
Func<int, int, int> addLambda = (c, d) => rld.add(c, d);
int result1 = addLambda(int.Parse(parameters[].ToString()), int.Parse(parameters[].ToString()));
Console.WriteLine("The add result from using the Lambda without tree is : {0}",result1); //利用委托调用add方法.
addDelegate addDelgateInstance = new addDelegate(rld.add);
int result2 = addDelgateInstance(int.Parse(parameters[].ToString()), int.Parse(parameters[].ToString()));
Console.WriteLine("The add result from using the delegate is : {0}", result2); //利用匿名方法调用add方法.
addDelegate AnonymousInstance = delegate(int a, int b) { return rld.add(a,b); };
int result3 = AnonymousInstance(int.Parse(parameters[].ToString()), int.Parse(parameters[].ToString()));
Console.WriteLine("The add result from using the anonymous method is : {0}", result3);
Console.ReadLine();
}
}
}

以上代码分别对委托、匿名方法、Lambda表达式、Lambda表达式树以及反射执行同一方法的过程进行比较,输出结果为:

最新文章

  1. MongoDB系列(一):简介及安装
  2. 烂泥:KVM、kickstart与nginx集成
  3. 【smarty项目源码】模拟smarty模版文件的解析过程
  4. Python 3 条件、循环和assert、pass、del
  5. SQL 数据库 函数
  6. pssh 不能执行指定用户命令
  7. 饭卡------HDOJ杭电2546(还是01背包!!!!!!)
  8. .bat脚本将windows server 2008设置成ntp时间同步服务器
  9. JQuery插件制作动态网页
  10. Java基础系列--final关键字
  11. python利用xlrd读取excel文件始终报错原因
  12. linux-shell系列4-init
  13. Linux hostname设置,静态ip设置,hostname与静态ip相互映射
  14. JVM探秘1--JVM内存运行时区域划分
  15. java.lang.IllegalStateException: Illegal access
  16. 转载-Mac下面的SecureCRT(附破解方案) 更新到最新的8.0.2
  17. 使用FindAncestor查找方式绑定且不需要使用datacontext
  18. python机器学习sklearn 岭回归(Ridge、RidgeCV)
  19. 什么是 SSO 与 CAS?
  20. 526. Beautiful Arrangement

热门文章

  1. 代码手写UI,xib和StoryBoard间的博弈,以及Interface Builder的一些小技巧
  2. TP3.2实例化复杂模型类
  3. JS操作frame框架
  4. logback+slf4j作为日志系统
  5. [译]流言终结者 —— SQL Server 是Sybase的产品而不是微软的
  6. 在js或css后加?v= 版本号不让浏览器缓存
  7. iOS设备的标识---UUID和IDFA
  8. 【php】基础学习4
  9. linux 硬件设备文件名
  10. Android中如何实现文件下载