gpt4 book ai didi

c# - Mono.Cecil 替换方法中的参数

转载 作者:太空宇宙 更新时间:2023-11-03 15:23:49 29 4
gpt4 key购买 nike

任务:

查找所有调用函数

public static void WriteString(int index0, string s, int index1)
{
Console.WriteLine(s);
}

在 SomeCnsl.exe 中并在函数 ChangeString 中包装参数 's'

public static string ChangeText(string text)
{
return text + "new";
}

示例:

original: WriteString(0,"hello",1);
wrap: WriteString(0,ChangeText("hello"),1);

为了解决这个任务,我使用 Mono.Cecil。我的解决方案如下所示:

private static AssemblyDefinition MainAssembly;
static void Main(string[] args)
{
MainAssembly = AssemblyDefinition.ReadAssembly("SomeCnsl.exe");

var changeTextMethod = typeof(SomeCnsl.Program).GetMethod("ChangeText");
var changeTextMethodRef = MainAssembly.MainModule.Import(changeTextMethod);

var mainMethod = MainAssembly.Modules.SelectMany(mod => ModuleDefinitionRocks.GetAllTypes(mod))
.SelectMany(t => t.Methods)
.Where(method => null != method.Body);

foreach (var body in mainMethod.Select(m => m.Body))
{
var processor = body.GetILProcessor();
var instructions = body.Instructions.Where(instr => instr.OpCode == OpCodes.Call && instr.ToString().Contains("WriteString")).ToList();
foreach (var instr in instructions)
{
var stringEndArg = GetStringArgument(instr);
var writeInstruction = processor.Create(OpCodes.Call, changeTextMethodRef);
processor.InsertAfter(stringEndArg, writeInstruction);
}
}
SavePatchedAssembly();
}

为了找到字符串参数,我创建了递归方法 GetStringArgument:

public static Instruction GetStringArgument(Instruction callDrawString)
{
if (callDrawString.Previous.OpCode == OpCodes.Ldstr || callDrawString.Previous.OpCode == OpCodes.Ldarg_1 ||
(callDrawString.Previous.OpCode == OpCodes.Call && callDrawString.Previous.ToString().Contains("System.String::")) ||
(callDrawString.Previous.OpCode == OpCodes.Callvirt && callDrawString.Previous.ToString().Contains("System.String::")) ||
(callDrawString.Previous.OpCode == OpCodes.Callvirt && callDrawString.Previous.ToString().Contains("Generic.List`1<System.String>::get_Item")) ||
(callDrawString.Previous.OpCode == OpCodes.Callvirt && callDrawString.Previous.ToString().Contains("Generic.Dictionary`2<") && callDrawString.Previous.ToString().Contains("System.String>::get_Item")) ||
((callDrawString.Previous.Operand as ParameterReference) != null && (callDrawString.Previous.Operand as ParameterReference).ParameterType.FullName == typeof(string).FullName) ||
((callDrawString.Previous.Operand as FieldReference) != null && (callDrawString.Previous.Operand as FieldReference).FieldType.FullName == typeof(string).FullName) ||
((callDrawString.Previous.Operand as PropertyReference) != null && (callDrawString.Previous.Operand as PropertyReference).PropertyType.FullName == typeof(string).FullName))
{
return callDrawString.Previous;
}
else
{
return GetStringArgument(callDrawString.Previous);
}
}

这是工作。直到在 WriteString 的参数中放入一些字符串,如下所示:

static Dictionary<string, int> listParam = new Dictionary<string, int> { { "first", 1 }, { "second", 2 }, { "third", 3 } };
static int index = 2;
static string indexString = "second";

static void Main(string[] args)
{
while(true)
{
Thread.Sleep(1000);
WriteString(index, indexString, listParam[indexString]);
}
}

ILCode WriteString call:
IL_0022: ldsfld int32 SomeCnsl.Program::index
IL_0027: ldsfld string SomeCnsl.Program::indexString
IL_002c: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2<string, int32> SomeCnsl.Program::listParam
IL_0031: ldsfld string SomeCnsl.Program::indexString
IL_0036: callvirt instance !1/*int32*/ class [mscorlib]System.Collections.Generic.Dictionary`2<string, int32>::get_Item(!0/*string*/)
IL_003b: call void SomeCnsl.Program::WriteString(string, int32, int32)
IL_0040: nop

所以,我的问题是:

我可以更精确地定义函数 WriteText 中第二个参数的所有 IL 命令吗?如果我可以,那么怎么做?

最佳答案

这不是您问题的答案,但可以帮助您以另一种方式来回答。您可以根据需要使用 Roslyn 重写代码。

可以通过继承CSharpSyntaxRewriting,然后重写相关方法来实现。之后您将收到一个包含修改后代码的新语法树,然后您可以对其进行编译并将其保存到磁盘。看看here例如。

关于c# - Mono.Cecil 替换方法中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36151536/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com