我正在使用 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));
}
}
}