靠近IL用DynamicMethod简单实现方法
2008-05-21 16:53
281 查看
最近一直在学习Emit,对指令有一些了解.总结了一些小经验在IL指令中经常的事情就
是把变量,参数推到堆栈上然后call一些方法,来来回回的这样做.下面贴个用DynamicMethod简单实现方法的代码:)
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
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();
}
}
}
当你熟了某些指令的时候,事情就变得简单并不是想象中复杂.
是把变量,参数推到堆栈上然后call一些方法,来来回回的这样做.下面贴个用DynamicMethod简单实现方法的代码:)
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
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简单实现方法
- CSS相册简单实现方法(功能分析及代码)
- JS简单实现点击复制链接的方法
- js 判断附件后缀的简单实现方法
- Java实现一个简单的缓存方法
- 非常简单的实现LINQ通用分页绑定方法
- 远程方法调用简单实现
- android http-post方法简单实现
- Java 多线程的两种简单实现方法
- 在PHP中实现单点登录(Single Sign On)的简单方法
- 简单方法实现webbrowser不显示图片flash的功能。
- js实现瀑布流的一种简单方法实例分享
- 在Python 3中实现类型检查器的简单方法
- JS简单实现点击按钮或文字显示遮罩层的方法
- Android简单PopWindow的实现方法。
- 一个简单的node.js界面实现方法
- JavaScript简单遍历DOM对象所有属性的实现方法
- 实现右滑关闭Activity的简单方法
- Winform消除button按下出现的虚线简单实现方法
- java设置一段代码执行的超时时间的简单方法 间接实现获取Connection超时问题