Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用dnlib添加对MessageBox.Show()的调用?_C#_.net_Opcode_Ilasm_Dnlib - Fatal编程技术网

C# 如何使用dnlib添加对MessageBox.Show()的调用?

C# 如何使用dnlib添加对MessageBox.Show()的调用?,c#,.net,opcode,ilasm,dnlib,C#,.net,Opcode,Ilasm,Dnlib,我正在尝试从dnlib调用MessageBox.Show。我有一个查找Show方法的函数: MethodDef GetSystemMethod(Type DeclaringType, string Methodname, Type[] MethodParams) { var filename = DeclaringType.Module.FullyQualifiedName; var mod = ModuleDefMD.Load(filename);

我正在尝试从dnlib调用MessageBox.Show。我有一个查找Show方法的函数:

MethodDef GetSystemMethod(Type DeclaringType, string Methodname, Type[] MethodParams)
    {
        var filename = DeclaringType.Module.FullyQualifiedName;
        var mod = ModuleDefMD.Load(filename);
        TypeDef[] types = mod.GetTypes().ToArray();
        foreach(TypeDef t in types)
        {
            if(t.Name==DeclaringType.Name)
            {
                foreach(var m in t.Methods)
                {
                    bool ok = true;
                    int i = 0;
                    foreach(var p in m.Parameters)
                    {
                        if (p.Type.TypeName != MethodParams[i].Name) ok = false;
                    }
                    if(ok)
                    {
                        return m;
                    }
                }
            }
        }
        throw new Exception("System Method not found!");
    }
我就是这样使用它的:

method.Body.Instructions.Clear();
                    method.Body.Instructions.Add(dnlib.DotNet.Emit.OpCodes.Nop.ToInstruction());
                    method.Body.Instructions.Add(dnlib.DotNet.Emit.OpCodes.Ldstr.ToInstruction("changed method here!"));
                    method.Body.Instructions.Add(dnlib.DotNet.Emit.OpCodes.Call.ToInstruction(GetSystemMethod(typeof(MessageBox), "Show", new Type[] { typeof(string)})));
                    method.Body.Instructions.Add(dnlib.DotNet.Emit.OpCodes.Pop.ToInstruction());
                    method.Body.Instructions.Add(dnlib.DotNet.Emit.OpCodes.Ret.ToInstruction());
                    module.Write(Path.GetDirectoryName(file) + "\\changed-" + Path.GetFileName(file));
当我运行它时,当它试图将模块写入文件时,会出现此异常。有人能看一下并解释一下我做错了什么吗

dnlib.DotNet.Writer.ModuleWriterException: 'Method System.Windows.Forms.HelpInfo System.Windows.Forms.MessageBox::get_HelpInfo() (06002BE3) is not defined in this module (TestProj.exe). A method was removed that is still referenced by this module.'

你解决了这个问题吗?没有,很不幸,我不知道为什么没有人回答。我觉得我问了一个愚蠢的问题。没有愚蠢的问题,试着把
GetSystemMethod
包装在
method.Module.Import()
里面看看它是否有效,像这样:
method.Body.Instructions.Add(dnlib.DotNet.Emit.OpCodes.Call.ToInstruction)(method.Module.Import(GetSystemMethod)(typeof(MessageBox),“Show”,new Type[]{typeof(string)}))));