靠近IL用DynamicMethod简单实现方法
2012-04-16 11:34
127 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DynamicMethod dynamicMethod = new DynamicMethod("Test",null,new Type[]{typeof(System.String)},typeof(System.String).Module);
ILGenerator iLGenerator = dynamicMethod.GetILGenerator();
iLGenerator.Emit(OpCodes.Ldarg_0);
MethodInfo methodInfo = typeof(Console).GetMethod("WriteLine",new Type[]{typeof(System.String)});
iLGenerator.Emit(OpCodes.Call,methodInfo); //不明白这句话的意思啊? iLGenerator.Emit(OpCodes.Ret);
Action<System.String> action = (Action<System.String>)dynamicMethod.CreateDelegate(typeof(Action<System.String>));
action("Herry");
/*
DynamicMethod dm = new DynamicMethod("Test", null,
new Type[] { typeof(string) }, typeof(string).Module);
ILGenerator il = dm.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);//把参数推到堆栈上
MethodInfo call = typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) });
il.Emit(OpCodes.Call, call);//执行Console.WriteLine方法
il.Emit(OpCodes.Ret);//结束返回
Action<string> test = (Action<string>)dm.CreateDelegate(typeof(Action<string>));
test("henry");
//下面Test1方法和Test完成的方法是一样的,但IL似乎有些不同.
//主要体现变量设置,对于变量的位置也会影响指令
dm = new DynamicMethod("Test1", null,
new Type[] { typeof(string) }, typeof(string).Module);
il = dm.GetILGenerator();
il.DeclareLocal(typeof(string));
il.Emit(OpCodes.Ldarg_0);//把参数推到堆栈上
il.Emit(OpCodes.Stloc_0);//把值保存到索引为0的变量里
il.Emit(OpCodes.Ldloc_0);//把索引为0的变量推到堆栈上
call = typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) });
il.Emit(OpCodes.Call, call);//执行Console.WriteLine方法
il.Emit(OpCodes.Ret);
test = (Action<string>)dm.CreateDelegate(typeof(Action<string>));
test("henry");
//对于下面的方法大家自己推一下,其实很简单.
//如果看起来有不明白,不防copy到vs.net上然后看指令描述信息:)
dm = new DynamicMethod("Test2", null,
new Type[] { typeof(string) }, typeof(string).Module);
il = dm.GetILGenerator();
il.DeclareLocal(typeof(string));
il.Emit(OpCodes.Ldstr, "你好 ");
il.Emit(OpCodes.Ldarg_0);
call = typeof(string).GetMethod("Concat", new Type[] { typeof(string), typeof(string) });
il.Emit(OpCodes.Call, call);
il.Emit(OpCodes.Stloc_0);
il.Emit(OpCodes.Ldloc_0);
call = typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) });
il.Emit(OpCodes.Call, call);
il.Emit(OpCodes.Ret);
test = (Action<string>)dm.CreateDelegate(typeof(Action<string>));
test("henry");*/
Console.Read();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DynamicMethod dynamicMethod = new DynamicMethod("Test",null,new Type[]{typeof(System.String)},typeof(System.String).Module);
ILGenerator iLGenerator = dynamicMethod.GetILGenerator();
iLGenerator.Emit(OpCodes.Ldarg_0);
MethodInfo methodInfo = typeof(Console).GetMethod("WriteLine",new Type[]{typeof(System.String)});
iLGenerator.Emit(OpCodes.Call,methodInfo); //不明白这句话的意思啊? iLGenerator.Emit(OpCodes.Ret);
Action<System.String> action = (Action<System.String>)dynamicMethod.CreateDelegate(typeof(Action<System.String>));
action("Herry");
/*
DynamicMethod dm = new DynamicMethod("Test", null,
new Type[] { typeof(string) }, typeof(string).Module);
ILGenerator il = dm.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);//把参数推到堆栈上
MethodInfo call = typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) });
il.Emit(OpCodes.Call, call);//执行Console.WriteLine方法
il.Emit(OpCodes.Ret);//结束返回
Action<string> test = (Action<string>)dm.CreateDelegate(typeof(Action<string>));
test("henry");
//下面Test1方法和Test完成的方法是一样的,但IL似乎有些不同.
//主要体现变量设置,对于变量的位置也会影响指令
dm = new DynamicMethod("Test1", null,
new Type[] { typeof(string) }, typeof(string).Module);
il = dm.GetILGenerator();
il.DeclareLocal(typeof(string));
il.Emit(OpCodes.Ldarg_0);//把参数推到堆栈上
il.Emit(OpCodes.Stloc_0);//把值保存到索引为0的变量里
il.Emit(OpCodes.Ldloc_0);//把索引为0的变量推到堆栈上
call = typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) });
il.Emit(OpCodes.Call, call);//执行Console.WriteLine方法
il.Emit(OpCodes.Ret);
test = (Action<string>)dm.CreateDelegate(typeof(Action<string>));
test("henry");
//对于下面的方法大家自己推一下,其实很简单.
//如果看起来有不明白,不防copy到vs.net上然后看指令描述信息:)
dm = new DynamicMethod("Test2", null,
new Type[] { typeof(string) }, typeof(string).Module);
il = dm.GetILGenerator();
il.DeclareLocal(typeof(string));
il.Emit(OpCodes.Ldstr, "你好 ");
il.Emit(OpCodes.Ldarg_0);
call = typeof(string).GetMethod("Concat", new Type[] { typeof(string), typeof(string) });
il.Emit(OpCodes.Call, call);
il.Emit(OpCodes.Stloc_0);
il.Emit(OpCodes.Ldloc_0);
call = typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) });
il.Emit(OpCodes.Call, call);
il.Emit(OpCodes.Ret);
test = (Action<string>)dm.CreateDelegate(typeof(Action<string>));
test("henry");*/
Console.Read();
}
}
}
相关文章推荐
- 靠近IL用DynamicMethod简单实现方法
- ASP.NET实现回调服务器的简单方法.
- jQuery实现“弹层即消失”的最简单方法(用于提示性的弹层)
- ajax的简单使用——get&post两种方法的实现(1)
- 简单的弹幕方法实现
- 同过增强Connection类[重写了close的方法]实现的从连接池取出连接并放回连接的简单的实现流程
- 同过增强Connection类[重写了close的方法]实现的从连接池取出连接并放回连接的简单实现
- 《大话设计模式》- 简单工厂方法 - C++实现
- 最简单实现跨域的方法:用 Nginx 反向代理
- ATL实现Connection Point的一种简单的方法
- javascript封装简单实现方法
- 简单的HTTP请求post,get,put,delete,head方法的实现
- 在Android中如何使用clipPath()方法实现简单的裁剪圆形图片
- ASP.NET实现公历转农历的简单方法
- android中图片翻页效果简单的实现方法
- Python使用shelve模块实现简单数据存储的方法
- thinkPHP3.2简单实现文件上传的方法
- VC中实现文字竖排的简单方法
- C++设置超时时间的简单实现方法