public bool RemoveModuleDef(ModuleDefMD module)
 {
     if (!NetModules.Remove(module.Name))
     {
         foreach (var assembly in LoadedAssemblies.Values)
         {
             foreach (var asmModule in assembly.Modules)
             {
                 if (asmModule == module)
                 {
                     if (assembly.Modules.Count == 1)
                     {
                         LoadedAssemblies.Remove(new AssemblyNameInfo(assembly));
                     }
                     else
                     {
                         assembly.Modules.Remove(asmModule);
                     }
                     Modules.Remove(module);
                     return(true);
                 }
             }
         }
         return(false);
     }
     Modules.Remove(module);
     return(true);
 }
        public bool AddModuleDef(ModuleDefMD module, out ModuleDefMD cached)
        {
            module.EnableTypeDefFindCache = true;
            if (module.Assembly == null)
            {
                foreach (var assembly in LoadedAssemblies)
                {
                    foreach (var m in assembly.Value.Modules)
                    {
                        if (m.Name == module.Name)
                        {
                            cached = (ModuleDefMD)m;
                            return(false);
                        }
                    }

                    var asmModule = (ModuleDefMD)assembly.Value.ManifestModule;
                    var rows      = asmModule.TablesStream.FileTable.Rows;
                    for (uint i = 1; i <= rows; i++)
                    {
                        var file = asmModule.TablesStream.ReadFileRow(i);
                        var name = asmModule.StringsStream.ReadNoNull(file.Name);
                        if (name == module.Name)
                        {
                            assembly.Value.Modules.Add(module);
                            Modules.Add(module);
                            cached = null;
                            return(true);
                        }
                    }
                }
                cached = null;
                if (NetModules.ContainsKey(module.Name))
                {
                    return(false);
                }
                NetModules.Add(module.Name, module);
                Modules.Add(module);
                return(true);
            }
            var         asmName = new AssemblyNameInfo(module.Assembly);
            AssemblyDef cachedAsm;

            if (LoadedAssemblies.TryGetValue(asmName, out cachedAsm))
            {
                cached = (ModuleDefMD)cachedAsm.ManifestModule;
                return(false);
            }
            LoadedAssemblies[asmName] = module.Assembly;
            Modules.Add(module);
            cached = null;
            return(true);
        }
        void ResolveNetModules(AssemblyDef assembly)
        {
            while (assembly.Modules.Count > 1)
            {
                assembly.Modules.RemoveAt(1);
            }

            var asmModule = (ModuleDefMD)assembly.ManifestModule;
            var rows      = asmModule.TablesStream.FileTable.Rows;

            for (uint i = 1; i <= rows; i++)
            {
                var         file = asmModule.TablesStream.ReadFileRow(i);
                var         name = asmModule.StringsStream.ReadNoNull(file.Name);
                ModuleDefMD loneModule;
                if (NetModules.TryGetValue(name, out loneModule))
                {
                    assembly.Modules.Add(loneModule);
                    NetModules.Remove(name);
                }
            }
        }