public ModuleMetadataInfo(Module module, Mono.Cecil.ModuleDefinition cecilModule)
 {
     this.Module      = module;
     this.CecilModule = cecilModule;
     typeRefLoader    = new CecilLoader();
     typeRefLoader.SetCurrentModule(cecilModule);
 }
Exemplo n.º 2
0
        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);
        }