/// <summary>
        /// Decompiles the method into C# code that cane be patched and replaced
        /// </summary>
        /// <param name="method"></param>
        /// <returns></returns>
        public static string Decompile(this MethodDefinition method)
        {
            var decompiler = new ICSharpCode.Decompiler.CSharp.CSharpDecompiler(method.Module, new ICSharpCode.Decompiler.DecompilerSettings()
            {
            });

            return(decompiler.DecompileAsString(new[]
            {
                method
            }));
        }
Пример #2
0
        public UtilitiesTests()
        {
            string currentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string assemblyPath     = Path.Combine(currentDirectory, "TestAssembly.dll");
            var    decompiler       = new ICSharpCode.Decompiler.CSharp.CSharpDecompiler(assemblyPath, new DecompilerSettings());

            var typeName = new FullTypeName("TestAssembly.TestClass");

            type     = decompiler.TypeSystem.MainModule.Compilation.FindType(typeName).GetDefinition();
            method   = type.Methods.First();
            field    = type.Fields.First();
            property = type.Properties.First();
        }
Пример #3
0
        public void Decompile(Stream assemblyStream, TextWriter codeWriter)
        {
            // ReSharper disable once AgentHeisenbug.CallToNonThreadSafeStaticMethodInThreadSafeType
            var readerParameters = new ReaderParameters {
                AssemblyResolver = _assemblyResolver
            };

            using (var module = ModuleDefinition.ReadModule(assemblyStream, readerParameters)) {
                var decompiler = new ICSharpCode.Decompiler.CSharp.CSharpDecompiler(module, DecompilerSettings);
                var syntaxTree = decompiler.DecompileWholeModuleAsSingleFile();

                new CSharpOutputVisitor(codeWriter, FormattingOptions).VisitSyntaxTree(syntaxTree);
            }
        }
Пример #4
0
        public string Dump()
        {
            using (var testEnvironment = RuntimeEnvironmentFactory.Create(_dependencies))
            {
                string             mainModuleFullName   = Emit(testEnvironment, manifestResources: null, EmitOptions.Default);
                IList <ModuleData> moduleDatas          = testEnvironment.GetAllModuleData();
                string             mainModuleSimpleName = moduleDatas.Single(md => md.FullName == mainModuleFullName).SimpleName;
                RuntimeEnvironmentUtilities.DumpAssemblyData(moduleDatas, out var dumpDir);

                string modulePath = Path.Combine(dumpDir, mainModuleSimpleName + ".dll");
                var    decompiler = new ICSharpCode.Decompiler.CSharp.CSharpDecompiler(modulePath, new ICSharpCode.Decompiler.DecompilerSettings());
                var    syntaxTree = decompiler.DecompileWholeModuleAsSingleFile();
                return(syntaxTree.ToString());
            }
        }
Пример #5
0
        public void Decompile(CompilationStreamPair streams, TextWriter codeWriter)
        {
            Argument.NotNull(nameof(streams), streams);
            Argument.NotNull(nameof(codeWriter), codeWriter);

            using (var assemblyFile = new PEFile("", streams.AssemblyStream))
                using (var debugInfo = streams.SymbolStream != null ? _debugInfoFactory(streams.SymbolStream) : null) {
                    var decompiler = new ICSharpCode.Decompiler.CSharp.CSharpDecompiler(assemblyFile, _assemblyResolver, DecompilerSettings)
                    {
                        DebugInfoProvider = debugInfo
                    };
                    var syntaxTree = decompiler.DecompileWholeModuleAsSingleFile();

                    new CSharpOutputVisitor(codeWriter, FormattingOptions).VisitSyntaxTree(syntaxTree);
                }
        }
Пример #6
0
        public void Decompile(CompilationStreamPair streams, TextWriter codeWriter)
        {
            Argument.NotNull(nameof(streams), streams);
            Argument.NotNull(nameof(codeWriter), codeWriter);

            var readerParameters = new ReaderParameters {
                AssemblyResolver = _assemblyResolver
            };

            using (var module = ModuleDefinition.ReadModule(streams.AssemblyStream, readerParameters)) {
                var decompiler = new ICSharpCode.Decompiler.CSharp.CSharpDecompiler(module, DecompilerSettings);
                var syntaxTree = decompiler.DecompileWholeModuleAsSingleFile();

                new CSharpOutputVisitor(codeWriter, FormattingOptions).VisitSyntaxTree(syntaxTree);
            }
        }
Пример #7
0
        public string Dump(string methodName = null)
        {
            using (var testEnvironment = RuntimeEnvironmentFactory.Create(_dependencies))
            {
                string             mainModuleFullName = Emit(testEnvironment, manifestResources: null, EmitOptions.Default);
                IList <ModuleData> moduleDatas        = testEnvironment.GetAllModuleData();
                var mainModule = moduleDatas.Single(md => md.FullName == mainModuleFullName);
                RuntimeEnvironmentUtilities.DumpAssemblyData(moduleDatas, out var dumpDir);

                string extension  = mainModule.Kind == OutputKind.ConsoleApplication ? ".exe" : ".dll";
                string modulePath = Path.Combine(dumpDir, mainModule.SimpleName + extension);

                var decompiler = new ICSharpCode.Decompiler.CSharp.CSharpDecompiler(modulePath,
                                                                                    new ICSharpCode.Decompiler.DecompilerSettings()
                {
                    AsyncAwait = false
                });

                if (methodName != null)
                {
                    var map = new Dictionary <string, ICSharpCode.Decompiler.TypeSystem.IMethod>();
                    listMethods(decompiler.TypeSystem.MainModule.RootNamespace, map);

                    if (map.TryGetValue(methodName, out var method))
                    {
                        return(decompiler.DecompileAsString(method.MetadataToken));
                    }
                    else
                    {
                        throw new Exception($"Didn't find method '{methodName}'. Available/distinguishable methods are: \r\n{string.Join("\r\n", map.Keys)}");
                    }
                }

                return(decompiler.DecompileWholeModuleAsString());
            }

            void listMethods(ICSharpCode.Decompiler.TypeSystem.INamespace @namespace, Dictionary <string, ICSharpCode.Decompiler.TypeSystem.IMethod> result)
            {
                foreach (var nestedNS in @namespace.ChildNamespaces)
                {
                    if (nestedNS.FullName != "System" &&
                        nestedNS.FullName != "Microsoft")
                    {
                        listMethods(nestedNS, result);
                    }
                }

                foreach (var type in @namespace.Types)
                {
                    listMethodsInType(type, result);
                }
            }

            void listMethodsInType(ICSharpCode.Decompiler.TypeSystem.ITypeDefinition type, Dictionary <string, ICSharpCode.Decompiler.TypeSystem.IMethod> result)
            {
                foreach (var nestedType in type.NestedTypes)
                {
                    listMethodsInType(nestedType, result);
                }

                foreach (var method in type.Methods)
                {
                    if (result.ContainsKey(method.FullName))
                    {
                        // There is a bug with FullName on methods in generic types
                        result.Remove(method.FullName);
                    }
                    else
                    {
                        result.Add(method.FullName, method);
                    }
                }
            }
        }