Mono.Cecil简介与示例
来源:互联网 发布:软件的性能指标 编辑:程序博客网 时间:2024/06/06 01:32
转载注明出处:点击打开链接
CLR自带的反射机智和API可以很轻松的读取.NET程序集信息,但是不能对程序集进行修改。CLR提供的是只读的API,但是开源项目Mono.Cecil不仅仅可以读取.NET程序集的元数据,还可以进行修改。
先写一个简单的Console exe程序,这里项目名称使用Cecil.Program:
using System;using System.Reflection;namespace Cecil.Program{ class Program { static void Main(string[] args) { TestType tt = new TestType(); tt.SayHello(); tt.AboutMe(); Console.ReadKey(); } } public class TestType { [Obsolete] public void SayHello() { Console.WriteLine("\tHello Cecil !"); } public void AboutMe() { Type type = typeof(TestType); MethodInfo method = type.GetMethod("SayHello"); if (method.IsVirtual) Console.WriteLine("\tI'm a virtual method"); else Console.WriteLine("\tI'm a non-virtual method"); object[] attributes = method.GetCustomAttributes(false); if (attributes != null && attributes.Length > 0) { Console.WriteLine("\tI have the following attributes:"); foreach (object attr in attributes) Console.WriteLine("\t\t" + attr.GetType().Name); } } }}这个程序集的运行结果如下:
方法SayHello的IL代码如下:
接下来使用另外一个Console exe程序来修改Cecil.Program.exe,项目名称使用Cecil:
using Mono.Cecil;using Mono.Cecil.Cil;AssemblyDefinition assembly = AssemblyFactory.GetAssembly("Cecil.Program.exe");TypeDefinition type = assembly.MainModule.Types["Cecil.Program.TestType"];MethodDefinition sayHello = null;foreach (MethodDefinition md in type.Methods) if (md.Name == "SayHello") sayHello = md;//Console.WriteLine(string value)方法MethodInfo writeLine = typeof(Console).GetMethod("WriteLine" , new Type[] { typeof(string) });//Console.WriteLine方法导入MainModule,并返回在AssemblyDefinition中的引用方式MethodReference writeLineRef = assembly.MainModule.Import(writeLine);//在SayHello方法开始位置插入一条trace语句// Console.WriteLine(">>Intercepting ");//如果插入的语句需要使用函数入参,则必须插入在OpCodes.Ldarg等指令之后CilWorker worker = sayHello.Body.CilWorker;Instruction ldstr = worker.Create(OpCodes.Ldstr, ">>Intercepting " + sayHello.Name);Instruction call = worker.Create(OpCodes.Call, writeLineRef);Instruction first = sayHello.Body.Instructions[0];worker.InsertBefore(first, call);worker.InsertBefore(call, ldstr);//在SayHello方法结束位置插入一条trace语句// Console.WriteLine(">>Intercepted ");//语句必须插入在OpCodes.Ret指令的前面int offset = sayHello.Body.Instructions.Count - 1;Instruction last = sayHello.Body.Instructions[offset--];while (last.OpCode == OpCodes.Nop || last.OpCode == OpCodes.Ret) last = sayHello.Body.Instructions[offset--];ldstr = worker.Create(OpCodes.Ldstr, ">>Intercepted " + sayHello.Name);worker.InsertAfter(last, ldstr);worker.InsertAfter(ldstr, call);//把SayHello方法改为虚方法sayHello.IsVirtual = true;//给SayHello方法添加一个SerializableAttributeCustomAttribute attribute = new CustomAttribute( assembly.MainModule.Import( typeof(SerializableAttribute).GetConstructor(Type.EmptyTypes)));sayHello.CustomAttributes.Add(attribute);AssemblyFactory.SaveAssembly(assembly, "Cecil.Program.modified.exe");Console.WriteLine("Assembly modified successfully!");Console.ReadKey();
编译生成Cecil.exe,然后把Cecil.Program.exe拷贝到这个目录下,运行Cecil.exe,便会在当前目录生成Cecil.Program.modified.exe,运行Cecil.Program.modified.exe结果如下:
修改后的方法SayHello的IL代码如下:
从上面的基本使用方法可以看出,Cecil的确是易于使用,对象模型结构非常实用,这里是官方网站的一个主要对象结构图:
IL指令的复杂性
在assembly、type、method级别上对程序集做修改是非常简单的,但是如果要修改方法体的IL代码,则可能会遇到一些较麻烦的事情,需要细致的处理
例如上面的SayHello方法如果是这样:
public void SayHello(bool print){ if (print) Console.WriteLine("\tHello Cecil !");}测试代码这样来调用:
TestType2 tt2 = new TestType2();tt2.SayHello(true);tt2.SayHello(false);Console.ReadKey();
其运行结果只会输出一条Hello Cecil !消息,仍然使用Cecil.exe来修改这个程序集,其运行结果如下图:
调用tt2.SayHello(false);时,应该也会有一个>>Intercepted SayHello消息,但是没有输出,对比一下IL代码就清楚了:
修改后的IL代码如下:
IL_000b那一句,为false时就直接跳转到IL_0021这个返回指令上了,不会输出Intercepted的消息
使用Mono.Cecil也可以修改这个跳转地址,例如:
//得到指令brfalse.sInstruction jmp = sayHello.Body.Instructions[1];....//把跳转的目标地址改成IL_0017 ldstr指令位置jmp.Operand = ldstr;
0 0
- Mono.Cecil简介与示例
- Mono.Cecil使用示例之获取源文件路径
- Mono.Cecil使用示例之给UnityEditor.dll中的ConsoleWindow添加双击委托
- Mono.Cecil使用示例之使指定程序集成为UnityEditor.dll的友元程序集
- Mono.Cecil 和MVC匿名用法
- 使用Mono.Cecil改变访问级别
- 编译时MSIL注入--实践Mono Cecil
- 使用Mono.Cecil对MSIL进行注入
- 使用Mono.Cecil对MSIL进行注入
- Mono.Cecil 初探(一):实现AOP
- 使用Mono.Cecil对MSIL进行注入
- 使用MOno Cecil 的相关开源项目
- 编译时MSIL注入--实践Mono Cecil(1)
- 编译时MSIL注入--实践Mono Cecil(1)
- 巧用Mono.Cecil反射加载类型和方法信息
- Mono简介
- Mono简介
- Mono简介
- 使用powerdesigner设计:概要设计 CDM模型
- 动态规划:从新手到专家
- java数据结构-HashMap
- 设计模式-11种行为型【占坑中】
- EasyUI(二)
- Mono.Cecil简介与示例
- CSS隐藏input光标
- PHP转码
- 【Algothrim】 FLoyd
- 从源码角度彻底分析layout_weight使用
- 根据PID号判断所属的容器
- 带有安全认证的webservice
- Google Protocol Buffer 的使用和原理
- 常用Oracle问题诊断sql脚本