标签:des style blog http io 使用 ar 文件 2014
1. 安装Mono.Cecil
2. 创建一个测试项目:
添加测试方法:
这个方法的返回值会被动态重写.
public class Class1
{
public static string Test()
{
return "ok";
}
}
3. code Injection 代码:
var path = @"\XX\bin\Debug\ClassLibrary1.dll";
var assembly = AssemblyDefinition.ReadAssembly
(path);
var type =assembly.MainModule.GetType("ClassLibrary1.Class1");
var foundMethod = type.GetMethods().First(m => m.Name =="Test");
////清空当前方法指令
foundMethod.Body.Instructions.Clear();
////获得当前IL的指令执行器
var worker = foundMethod.Body.GetILProcessor();
////修改返回值
Instruction ins1 = worker.Create(OpCodes.Ldstr, "will be changed onnext time run");
Instruction ins2 = worker.Create(OpCodes.Ret);
worker.Append(ins1);
worker.Append(ins2);
////保存DLL文件
assembly.Write(path);
////下次调用就会看到(因为新保存的DLL下次才能加载到)
Console.WriteLine(Class1.Test());
Console.Read();标签:des style blog http io 使用 ar 文件 2014
原文地址:http://blog.csdn.net/lan_liang/article/details/39401067