예제 #1
0
 /// <summary>
 /// Instianciates a CSharpDecompiler from a given assemblyfilename and applies decompilersettings
 /// </summary>
 /// <param name="assemblyFileName">path for assemblyfile to decompile</param>
 /// <param name="settings">optional decompiler settings</param>
 /// <returns>Instance of CSharpDecompiler</returns>
 public CSharpDecompiler GetCSharpDecompiler(string assemblyFileName, DecompilerSettings settings = null)
 {
     if (settings == null)
     {
         settings = new DecompilerSettings();
     }
     using (var file = new FileStream(assemblyFileName, FileMode.Open, FileAccess.Read))
     {
         var module   = new PEFile(assemblyFileName, file, PEStreamOptions.PrefetchEntireImage);
         var resolver = new UniversalAssemblyResolver(assemblyFileName, false,
                                                      module.Reader.DetectTargetFrameworkId(), PEStreamOptions.PrefetchMetadata);
         resolver.AddSearchDirectory(Path.GetDirectoryName(module.FullName));
         var typeSystem = new DecompilerTypeSystem(module, resolver, settings);
         var decompiler = new CSharpDecompiler(typeSystem, settings);
         decompiler.DebugInfoProvider = DebugInfoLoader.LoadSymbols(module);
         return(decompiler);
     }
 }
예제 #2
0
        PEFile LoadAssembly(object state)
        {
            MetadataReaderOptions options;

            if (DecompilerSettingsPanel.CurrentDecompilerSettings.ApplyWindowsRuntimeProjections)
            {
                options = MetadataReaderOptions.ApplyWindowsRuntimeProjections;
            }
            else
            {
                options = MetadataReaderOptions.None;
            }

            PEFile module;

            // runs on background thread
            if (state is Stream stream)
            {
                // Read the module from a precrafted stream
                module = new PEFile(fileName, stream, metadataOptions: options);
            }
            else
            {
                // Read the module from disk (by default)
                stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                module = new PEFile(fileName, stream, PEStreamOptions.PrefetchEntireImage,
                                    metadataOptions: options);
            }

            if (DecompilerSettingsPanel.CurrentDecompilerSettings.UseDebugSymbols)
            {
                try {
                    this.debugInfoProvider = DebugInfoLoader.LoadSymbols(module);
                } catch (IOException) {
                } catch (UnauthorizedAccessException) {
                } catch (InvalidOperationException) {
                    // ignore any errors during symbol loading
                }
            }
            lock (loadedAssemblies) {
                loadedAssemblies.Add(module, this);
            }
            return(module);
        }