使用 Cecil 修改 .Net 程序集

  Cecil 是 Mono 的一个子项目,用于对程序集进行读写,并且已经用于 Mono 的调试,Reflector 也使用它作为底层库。最近把 DbEntry 使用 Emit 生成程序集的方式,改成了使用 Cecil 的方式,就我的感受来说,Cecil 是比较优秀的,有一些地方,比 Emit 使用起来还舒服的多;不过,有一些地方也比较繁琐。

 

  我使用的是 Git 里的最新版本,如果大家要测试的话,也建议使用 Git 版,所以,需要安装一个 Git 客户端。

 

  这里,用一个非常简单的例子,说明一下 Cecil 的基本用法。

 

  首先,我们编写一个测试用的程序集 TestApp.exe :

 

using System;

namespace TestApp
{
class Program
{
static void Main()
{
Console.WriteLine(
"Main");
}

private static void Before()
{
Console.WriteLine(
"Before");
}

private static void After()
{
Console.WriteLine(
"After");
}
}
}

 


  然后,编写一个使用 Cecil 进行改写的应用 CecilTest.exe :

 

using System;
using System.Linq;
using Mono.Cecil;
using Mono.Cecil.Cil;

namespace CecilTest
{
class Program
{
static void Main(string[] args)
{
if(args.Length != 1)
{
Console.WriteLine(
"Usage: CecilTest TestApp.exe");
}

var m
= ModuleDefinition.ReadModule(args[0]);

var prog
= m.Types.First(p => p.Name == "Program");
var main
= prog.Methods.First(p => p.Name == "Main");
var before
= prog.Methods.First(p => p.Name == "Before");
var after
= prog.Methods.First(p => p.Name == "After");

var il
= main.Body.GetILProcessor();
il.InsertBefore(main.Body.Instructions[
0], il.Create(OpCodes.Call, before));
il.InsertBefore(main.Body.Instructions.Last(), il.Create(OpCodes.Call, after));

m.Write(args[
0] + ".exe");

Console.WriteLine(
"Done");
Console.ReadLine();
}
}
}

 


  编译这两个项目,并且使用 CecilTest.exe 处理一下 TestApp.exe,生成 TestApp.exe.exe,运行 TestApp.exe,运行结果:
Main

 

  运行 TestApp.exe.exe,运行结果:

Before
Main
After

  可以看到,我们已经成功的在 Main 函数的前后,分别插入了一次函数调用。

 

  基本使用方法就是这样,大体和 Emit 类似,只是不止可以使用 Emit 函数,还可以直接修改程序集。当然,还有其它一些细节的不同,比如它的变量定义不是通过 ILProcessor,而是直接操作函数体的 Variables;再比如它没有 DeclareLabel 函数,跳转直接引用函数体的 Instruction 进行。另外,它可以只加载目标程序集,却不加载其依赖项,所以很多东西都分定义和引用两种。

 

  就目前来说,我认为它的效果是令人满意的。不过它最大的问题,在于泛型处理上。不是说不能做,而是太繁琐,有时候甚至是不可能。在 DbEntry 中,最后被泛型击败,采取了 Reflection+Cecil 的方式,这种方式简单易行,不过问题是,Reflection 需要加载程序集,除了可能出现无法加载依赖项的异常外,也无法简单的回写原文件。我在 Cecil 的 Git 上提交了 issue,不过作者回复,不觉得这是问题,所以我也懒得纠缠了。

posted @ 2010-07-05 21:45  梁利锋  阅读(2221)  评论(3编辑  收藏  举报