我正在使用dnlib,我想在另一个可执行文件的Console.WriteLines末尾插入Console.ReadKey()。
以下代码部分有效。虽然问题是。它产生MSIL:
IL_0028: call void [mscorlib]System.Console::ReadKey()
当我希望它产生
时IL_015a: call valuetype [mscorlib]System.ConsoleKeyInfo [mscorlib]System.Console::ReadKey()
代码
ModuleDefMD mod = ModuleDefMD.Load(args[0]);
ModuleDef mode = new ModuleDefUser(args[0]);
Importer importer = new Importer(mod);
AssemblyDef asmr = mod.Assembly;
foreach (TypeDef type in mod.GetTypes())
{
foreach (MethodDef Method in type.Methods)
{
if (Method.Name == "Main")
{
TypeRef consoleRef = new TypeRefUser(mod, "System", "Console", mod.CorLibTypes.AssemblyRef);
MemberRef ReadAll = new MemberRefUser(mod, "ReadKey",
MethodSig.CreateStatic(mod.CorLibTypes.Void),
consoleRef);
var method = Method;
var instructions = method.Body.Instructions;
instructions.Insert(8, new Instruction(OpCodes.Call, ReadAll));
}
}
}