使用dnlib注入类型和方法时,父级不存在错误

问题描述

我一直在努力注入一种类型和方法来度过美好的一天。嗯以下是一些代码:我基本上只是想用Simpy.Simp替换System.Convert.FromBase64String,但我似乎无法使其正常工作...(我使用了https://github.com/yck1509/ConfuserEx/blob/master/Confuser.Core/Helpers/InjectHelper.cs的InjectHelper.cs )和来自https://github.com/yck1509/dnlib/tree/532c767a9a4f6af51cd4eb5d1c0af750c8509c5d的经过修改的dnlib)

ModuleDefMD moduleDefMD = ModuleDefMD.Load(typeof(runtime).Module);
        TypeDef type = new TypeDefUser("Simpy");
        type.Attributes = TypeAttributes.AutoLayout | TypeAttributes.Class;
        moduleDef.Types.Add(type);
        TypeDef typeDef = moduleDefMD.ResolveTypeDef(MDToken.ToRID(typeof(Simpy).MetadataToken));
        //InjectHelper.Inject(typeDef,type,moduleDef);
        IEnumerable<IDnlibDef> source = InjectHelper.Inject(typeDef,moduleDef);
        init = (MethodDef)source.Single((IDnlibDef method) => method.Name == "Simp");
        foreach (TypeDef typeDef2 in moduleDef.Types)
        {
            foreach (MethodDef methodDef in typeDef2.Methods)
            {
                bool flag = methodDef.Body == null;
                if (!flag)
                {
                    methodDef.Body.SimplifyBranches();
                    for (int i = 0; i < methodDef.Body.Instructions.Count; i++)
                    {
                        bool flag2 = methodDef.Body.Instructions[i].OpCode == OpCodes.Ldstr;
                        if (flag2)
                        {
                            string s = methodDef.Body.Instructions[i].Operand.ToString();
                            string text = Convert.ToBase64String(Encoding.UTF8.GetBytes(s));
                            methodDef.Body.Instructions[i].OpCode = OpCodes.nop;
                            methodDef.Body.Instructions.Insert(i + 1,new Instruction(OpCodes.Call,moduleDef.Import(typeof(Encoding).getmethod("get_UTF8",new Type[0]))));
                            methodDef.Body.Instructions.Insert(i + 2,new Instruction(OpCodes.Ldstr,text));
                            methodDef.Body.Instructions.Insert(i + 3,init));
                            methodDef.Body.Instructions.Insert(i + 4,new Instruction(OpCodes.Callvirt,moduleDef.Import(typeof(Encoding).getmethod("GetString",new Type[]
                            {
                                typeof(byte[])
                            }))));
                            i += 4;
                        }
                    }
                }
            }
        }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)