示例#1
0
        private static void InvokeDelegates(IList <TypeDef> typeDefs, MethodInfo invokeMethod, object invokeField)
        {
            var methodDefs = typeDefs.SelectMany(x => x.Methods).Where(x =>
                                                                       x.Module.Name == Assembly.ManifestModule.ScopeName && x.HasBody &&
                                                                       x.Body.Instructions.Count > 2 &&
                                                                       x.Body.Instructions[0].OpCode == OpCodes.Ldsfld &&
                                                                       x.Body.Instructions[0].Operand.ToString().Contains("Invoke") &&
                                                                       x.Body.Instructions[1].IsLdcI4());

            foreach (var methodDef in methodDefs)
            {
                _totalPackedMethods++;

                CurrentMethod     = methodDef;
                CurrentMethodBase = Assembly.ManifestModule.ResolveMethod(methodDef.MDToken.ToInt32());

                var mdToken = ((IType)methodDef.Body.Instructions[3].Operand).MDToken.ToInt32();
                JunkType.Add(methodDef.DeclaringType.NestedTypes.FirstOrDefault(net => net.MDToken.ToInt32() == mdToken));
                var index = methodDef.Body.Instructions[1].GetLdcI4Value();
                if (index == IgnoreIndex)
                {
                    continue;
                }

                var method = invokeMethod.Invoke(invokeField, new object[] { index });

                try
                {
                    var dynamicMethodBodyReader = new DynamicMethodBodyReader(AssemblyWriter.moduleDef, method);
                    dynamicMethodBodyReader.Read();
                    var unpackedMethod = dynamicMethodBodyReader.GetMethod();
                    AssemblyWriter.WriteMethod(methodDef, unpackedMethod);
                    _totalUnpackedMethods++;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error in Read(): " + ex.Message + "\nMethod : " + method);
                }
                finally
                {
                    CurrentMethod = null;
                }
            }
        }