/// <summary> /// this gives you SymbolTreeInfo for a metadata /// </summary> public static async Task<SymbolTreeInfo> GetInfoForMetadataReferenceAsync( Solution solution, PortableExecutableReference reference, bool loadOnly, CancellationToken cancellationToken) { var metadata = reference.GetMetadata(); if (metadata == null) { return null; } // Find the lock associated with this piece of metadata. This way only one thread is // computing a symbol tree info for a particular piece of metadata at a time. var gate = s_metadataIdToGate.GetValue(metadata.Id, s_metadataIdToGateCallback); using (await gate.DisposableWaitAsync(cancellationToken).ConfigureAwait(false)) { cancellationToken.ThrowIfCancellationRequested(); SymbolTreeInfo info; if (s_metadataIdToInfo.TryGetValue(metadata.Id, out info)) { return info; } info = await LoadOrCreateMetadataSymbolTreeInfoAsync( solution, reference, loadOnly, cancellationToken: cancellationToken).ConfigureAwait(false); if (info == null && loadOnly) { return null; } return s_metadataIdToInfo.GetValue(metadata.Id, _ => info); } }
private void WritePortableExecutableReferenceTo( PortableExecutableReference reference, ObjectWriter writer, CancellationToken cancellationToken) { WritePortableExecutableReferenceHeaderTo(reference, SerializationKinds.Bits, writer, cancellationToken); WriteTo(reference.GetMetadata(), writer, cancellationToken); // TODO: what I should do with documentation provider? it is not exposed outside }
internal static PEAssemblySymbol Create(PortableExecutableReference reference) { var data = (AssemblyMetadata)reference.GetMetadata(); //var data = AssemblyMetadata.CreateFromFile(reference.FilePath); var ass = data.GetAssembly(); return(new PEAssemblySymbol( ass, DocumentationProvider.Default, true, IsPchpCor(ass) ? MetadataImportOptions.Internal : MetadataImportOptions.Public)); }
internal static PEAssemblySymbol Create(PortableExecutableReference reference, PEAssembly ass = null) { if (ass == null) { ass = ((AssemblyMetadata)reference.GetMetadata()).GetAssembly(); } return(new PEAssemblySymbol( ass, reference.DocumentationProvider, reference.FilePath, true, IsPchpCor(ass) ? MetadataImportOptions.Internal : MetadataImportOptions.Public)); }
private static Metadata GetMetadataNoThrow(PortableExecutableReference reference) { try { return reference.GetMetadata(); } catch (Exception e) when (e is BadImageFormatException || e is IOException) { return null; } }
private static Metadata GetMetadataNoThrow(PortableExecutableReference reference) { try { return(reference.GetMetadata()); } catch (Exception e) when(e is BadImageFormatException || e is IOException) { return(null); } }
private static Metadata TryGetMetadata(PortableExecutableReference reference) { try { return(reference.GetMetadata()); } catch { // we have a reference but the file the reference is pointing to // might not actually exist on disk. // in that case, rather than crashing, we will handle it gracefully. return(null); } }
/// <summary> /// If the extended <paramref name="reference"/> is a .Net Assembly return a <see cref="MetadataReader"/> /// instance that can be used to read the assembly's embedded metadata, otherwise return null. /// </summary> /// <param name="reference"> The extended <see cref="PortableExecutableReference"/>. </param> /// <returns> A <see cref="MetadataReader"/> if the reference is a .Net assembly or null otherwise. </returns> public static MetadataReader?GetMetadataReader(this PortableExecutableReference reference) { try { if (reference.GetMetadata() is AssemblyMetadata assembly) { foreach (var module in assembly.GetModules()) { return(module.GetMetadataReader()); } } else if (reference.GetMetadata() is ModuleMetadata module) { return(module.GetMetadataReader()); } return(null); } catch (BadImageFormatException) { return(null); } }
public static ModuleMetadata GetManifestModuleMetadata(this PortableExecutableReference peReference) { switch (peReference.GetMetadata()) { case AssemblyMetadata assemblyMetadata: { if (assemblyMetadata.GetModules() is { Length : 1 } modules) { return(modules[0]); } } break; case ModuleMetadata moduleMetadata: return(moduleMetadata); } throw new InvalidOperationException(); }
private static SymbolTreeInfo CreateMetadataSymbolTreeInfo( Solution solution, VersionStamp version, PortableExecutableReference reference, CancellationToken cancellationToken) { var unsortedNodes = new List<Node> { new Node("", Node.RootNodeParentIndex) }; foreach (var moduleMetadata in GetModuleMetadata(reference.GetMetadata())) { MetadataReader reader; try { reader = moduleMetadata.GetMetadataReader(); } catch (BadImageFormatException) { continue; } GenerateMetadataNodes(reader, unsortedNodes); } return CreateSymbolTreeInfo(solution, version, reference.FilePath, unsortedNodes); }
private void WriteMvidsTo(PortableExecutableReference reference, ObjectWriter writer, CancellationToken cancellationToken) { var metadata = reference.GetMetadata(); var assemblyMetadata = metadata as AssemblyMetadata; if (assemblyMetadata != null) { writer.WriteInt32((int)assemblyMetadata.Kind); var modules = assemblyMetadata.GetModules(); writer.WriteInt32(modules.Length); foreach (var module in modules) { WriteMvidTo(module, writer, cancellationToken); } return; } WriteMvidTo((ModuleMetadata)metadata, writer, cancellationToken); }
private static Metadata TryGetMetadata(PortableExecutableReference reference) { try { return reference.GetMetadata(); } catch { // we have a reference but the file the reference is pointing to // might not actually exist on disk. // in that case, rather than crashing, we will handle it gracefully. return null; } }
public unsafe static ModuleInstance Create(PortableExecutableReference reference) { // make a copy of the metadata, so that we don't dispose the metadata of a reference that are shared accross tests: return Create(reference.GetMetadata(), symReader: null, includeLocalSignatures: false); }
public static ModuleInstance Create(PortableExecutableReference reference) { // make a copy of the metadata, so that we don't dispose the metadata of a reference that are shared accross tests: return(Create(reference.GetMetadata(), symReader: null, includeLocalSignatures: false)); }
public static InspectionResult ExtractIl(string projectJsonPath, string classFilename) { if (string.IsNullOrEmpty(projectJsonPath)) { throw new ArgumentNullException(nameof(projectJsonPath)); } if (string.IsNullOrEmpty(classFilename)) { throw new ArgumentNullException(nameof(classFilename)); } Compilation workspaceCompilation = WorkspaceManager.LoadWorkspace(projectJsonPath); var inspectionResult = new InspectionResult(); using (var stream = new MemoryStream()) { var compilationResult = workspaceCompilation.Emit(stream); if (!compilationResult.Success) { var errors = compilationResult.Diagnostics.Where(x => x.Severity == DiagnosticSeverity.Error).ToList(); foreach (var error in errors) { inspectionResult.AddError(error.GetMessage()); Console.WriteLine(error.ToString()); } if (inspectionResult.HasErrors) { return(inspectionResult); } } stream.Seek(0, SeekOrigin.Begin); //AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(stream); PortableExecutableReference ref2 = MetadataReference.CreateFromStream(stream); var syntaxTree2 = workspaceCompilation.SyntaxTrees.Where(x => x.FilePath.Contains(classFilename)); var trees = workspaceCompilation.SyntaxTrees.ToList(); var allTrees = trees.Where(x => x.FilePath.Contains(classFilename + ".cs")).ToList(); //TODO: optimise this var syntaxTree = allTrees.FirstOrDefault(x => x.FilePath.Contains("\\" + classFilename + ".cs")); if (syntaxTree == null) { syntaxTree = allTrees.FirstOrDefault(x => x.FilePath.Contains("/" + classFilename + ".cs")); } var sourceCode = syntaxTree.GetText().ToString(); var metadataReferences = workspaceCompilation.References.ToList(); var res3 = ref2.GetMetadata(); metadataReferences.Add(ref2); InspectionResult finalResult = CompileInMemory(sourceCode, metadataReferences, classFilename, inspectionResult); return(finalResult); } }