LoadedAssembly LoadAssembly(FileName fileName, CancellationToken cancellationToken, bool includeInternalMembers) { DateTime lastWriteTime = File.GetLastWriteTimeUtc(fileName); string cacheFileName = GetCacheFileName(fileName); LoadedAssembly pc = TryReadFromCache(cacheFileName, lastWriteTime); if (pc != null) { if (!includeInternalMembers || includeInternalMembers == pc.HasInternalMembers) { return(pc); } } //LoggingService.Debug("Loading " + fileName); cancellationToken.ThrowIfCancellationRequested(); var param = new ReaderParameters(); param.AssemblyResolver = new DummyAssemblyResolver(); ModuleDefinition module = ModuleDefinition.ReadModule(fileName, param); CecilLoader l = new CecilLoader(); l.IncludeInternalMembers = includeInternalMembers; string xmlDocFile = FindXmlDocumentation(fileName, module.Runtime); if (xmlDocFile != null) { try { l.DocumentationProvider = new XmlDocumentationProvider(xmlDocFile); } catch (XmlException ex) { LoggingService.Warn("Ignoring error while reading xml doc from " + xmlDocFile, ex); } catch (IOException ex) { LoggingService.Warn("Ignoring error while reading xml doc from " + xmlDocFile, ex); } catch (UnauthorizedAccessException ex) { LoggingService.Warn("Ignoring error while reading xml doc from " + xmlDocFile, ex); } } l.CancellationToken = cancellationToken; var references = module.AssemblyReferences .Select(anr => new DomAssemblyName(anr.FullName)); pc = new LoadedAssembly(l.LoadModule(module), lastWriteTime, includeInternalMembers, references); SaveToCacheAsync(cacheFileName, lastWriteTime, pc).FireAndForget(); //SaveToCache(cacheFileName, lastWriteTime, pc); return(pc); }
public DecompilerTypeSystem(ModuleDefinition moduleDefinition) { if (moduleDefinition == null) { throw new ArgumentNullException(nameof(moduleDefinition)); } this.moduleDefinition = moduleDefinition; CecilLoader cecilLoader = new CecilLoader { IncludeInternalMembers = true, LazyLoad = true, OnEntityLoaded = StoreMemberReference, ShortenInterfaceImplNames = false }; typeReferenceCecilLoader.SetCurrentModule(moduleDefinition); IUnresolvedAssembly mainAssembly = cecilLoader.LoadModule(moduleDefinition); // Load referenced assemblies and type-forwarder references. // This is necessary to make .NET Core/PCL binaries work better. var referencedAssemblies = new List <IUnresolvedAssembly>(); var assemblyReferenceQueue = new Queue <AssemblyNameReference>(moduleDefinition.AssemblyReferences); var processedAssemblyReferences = new HashSet <AssemblyNameReference>(KeyComparer.Create((AssemblyNameReference reference) => reference.FullName)); while (assemblyReferenceQueue.Count > 0) { var asmRef = assemblyReferenceQueue.Dequeue(); if (!processedAssemblyReferences.Add(asmRef)) { continue; } var asm = moduleDefinition.AssemblyResolver.Resolve(asmRef); if (asm != null) { referencedAssemblies.Add(cecilLoader.LoadAssembly(asm)); foreach (var forwarder in asm.MainModule.ExportedTypes) { if (!forwarder.IsForwarder || !(forwarder.Scope is AssemblyNameReference forwarderRef)) { continue; } assemblyReferenceQueue.Enqueue(forwarderRef); } } } compilation = new SimpleCompilation(mainAssembly, referencedAssemblies); // Primitive types are necessary to avoid assertions in ILReader. // Fallback to MinimalCorlib to provide the primitive types. if (compilation.FindType(KnownTypeCode.Void).Kind == TypeKind.Unknown || compilation.FindType(KnownTypeCode.Int32).Kind == TypeKind.Unknown) { referencedAssemblies.Add(MinimalCorlib.Instance); compilation = new SimpleCompilation(mainAssembly, referencedAssemblies); } context = new SimpleTypeResolveContext(compilation.MainAssembly); }
static IUnresolvedAssembly LoadModule(Module module, string fileName) { var param = new Mono.Cecil.ReaderParameters { AssemblyResolver = new DummyAssemblyResolver() }; var cecilModule = Mono.Cecil.ModuleDefinition.ReadModule(fileName, param); var moduleMetadataInfo = new ModuleMetadataInfo(module, cecilModule); var loader = new CecilLoader(); loader.IncludeInternalMembers = true; loader.LazyLoad = true; loader.OnEntityLoaded = moduleMetadataInfo.AddMember; var asm = loader.LoadModule(cecilModule); weakTable.Add(asm, moduleMetadataInfo); return(asm); }