private EcmaModule AddModule(string filePath, string expectedSimpleName) { MemoryMappedViewAccessor mappedViewAccessor = null; PdbSymbolReader pdbReader = null; try { PEReader peReader = OpenPEFile(filePath, out mappedViewAccessor); pdbReader = OpenAssociatedSymbolFile(filePath); EcmaModule module = EcmaModule.Create(this, peReader, pdbReader); MetadataReader metadataReader = module.MetadataReader; string simpleName = metadataReader.GetString(metadataReader.GetAssemblyDefinition().Name); if (expectedSimpleName != null && !simpleName.Equals(expectedSimpleName, StringComparison.OrdinalIgnoreCase)) { throw new FileNotFoundException("Assembly name does not match filename " + filePath); } ModuleData moduleData = new ModuleData() { SimpleName = simpleName, FilePath = filePath, Module = module, MappedViewAccessor = mappedViewAccessor }; lock (this) { ModuleData actualModuleData = _simpleNameHashtable.AddOrGetExisting(moduleData); if (actualModuleData != moduleData) { if (actualModuleData.FilePath != filePath) { throw new FileNotFoundException("Module with same simple name already exists " + filePath); } return(actualModuleData.Module); } mappedViewAccessor = null; // Ownership has been transfered pdbReader = null; // Ownership has been transferred _moduleHashtable.AddOrGetExisting(moduleData); } return(module); } finally { if (mappedViewAccessor != null) { mappedViewAccessor.Dispose(); } if (pdbReader != null) { pdbReader.Dispose(); } } }
private EcmaModule AddModule(string filePath, string expectedSimpleName, byte[] moduleDataBytes, bool useForBinding) { MemoryMappedViewAccessor mappedViewAccessor = null; PdbSymbolReader pdbReader = null; try { PEReader peReader = OpenPEFile(filePath, moduleDataBytes, out mappedViewAccessor); pdbReader = OpenAssociatedSymbolFile(filePath, peReader); EcmaModule module = EcmaModule.Create(this, peReader, containingAssembly: null, pdbReader); MetadataReader metadataReader = module.MetadataReader; string simpleName = metadataReader.GetString(metadataReader.GetAssemblyDefinition().Name); ModuleData moduleData = new ModuleData() { SimpleName = simpleName, FilePath = filePath, Module = module, MappedViewAccessor = mappedViewAccessor }; lock (this) { if (useForBinding) { ModuleData actualModuleData = _simpleNameHashtable.AddOrGetExisting(moduleData); if (actualModuleData != moduleData) { if (actualModuleData.FilePath != filePath) { throw new FileNotFoundException("Module with same simple name already exists " + filePath); } return(actualModuleData.Module); } } mappedViewAccessor = null; // Ownership has been transfered pdbReader = null; // Ownership has been transferred _moduleHashtable.AddOrGetExisting(moduleData); } return(module); } finally { if (mappedViewAccessor != null) { mappedViewAccessor.Dispose(); } if (pdbReader != null) { pdbReader.Dispose(); } } }
private EcmaModule AddModule(string filePath, string expectedSimpleName, bool useForBinding, ModuleData oldModuleData = null, bool throwOnFailureToLoad = true) { filePath = Path.GetFullPath(filePath); PEReader peReader = null; MemoryMappedViewAccessor mappedViewAccessor = null; PdbSymbolReader pdbReader = null; try { if (oldModuleData == null) { peReader = OpenPEFile(filePath, out mappedViewAccessor); #if !READYTORUN if (peReader.HasMetadata && (peReader.PEHeaders.CorHeader.Flags & (CorFlags.ILLibrary | CorFlags.ILOnly)) == 0) { throw new NotSupportedException($"Error: C++/CLI is not supported: '{filePath}'"); } #endif pdbReader = PortablePdbSymbolReader.TryOpenEmbedded(peReader, GetMetadataStringDecoder()) ?? OpenAssociatedSymbolFile(filePath, peReader); } else { filePath = oldModuleData.FilePath; peReader = oldModuleData.Module.PEReader; mappedViewAccessor = oldModuleData.MappedViewAccessor; pdbReader = oldModuleData.Module.PdbReader; } if (!peReader.HasMetadata && !throwOnFailureToLoad) { return(null); } EcmaModule module = EcmaModule.Create(this, peReader, containingAssembly: null, pdbReader); MetadataReader metadataReader = module.MetadataReader; string simpleName = metadataReader.GetString(metadataReader.GetAssemblyDefinition().Name); if (expectedSimpleName != null && !simpleName.Equals(expectedSimpleName, StringComparison.OrdinalIgnoreCase)) { throw new FileNotFoundException("Assembly name does not match filename " + filePath); } ModuleData moduleData = new ModuleData() { SimpleName = simpleName, FilePath = filePath, Module = module, MappedViewAccessor = mappedViewAccessor }; lock (this) { if (useForBinding) { ModuleData actualModuleData = _simpleNameHashtable.AddOrGetExisting(moduleData); if (actualModuleData != moduleData) { if (actualModuleData.FilePath != filePath) { throw new FileNotFoundException("Module with same simple name already exists " + filePath); } return(actualModuleData.Module); } } mappedViewAccessor = null; // Ownership has been transfered pdbReader = null; // Ownership has been transferred _moduleHashtable.AddOrGetExisting(moduleData); } return(module); } finally { if (mappedViewAccessor != null) { mappedViewAccessor.Dispose(); } if (pdbReader != null) { pdbReader.Dispose(); } } }