/// <summary>
        /// Resolves a command line metadata reference with the given <paramref name="resolvedFullPath"/> to a <see cref="MetadataReference"/>.
        /// </summary>
        private static MetadataReference ResolveMetadataFile(
            string resolvedFullPath,
            MetadataReferenceProperties properties,
            MetadataReferenceProvider metadataProvider,
            string display,
            List <DiagnosticInfo> diagnosticsOpt,
            CommonMessageProvider messageProviderOpt)
        {
            Debug.Assert(resolvedFullPath != null);
            Debug.Assert(PathUtilities.IsAbsolute(resolvedFullPath));
            Debug.Assert(display != null);

            if (diagnosticsOpt == null)
            {
                return(metadataProvider.GetReference(resolvedFullPath, properties));
            }

            MetadataReference reference;
            Diagnostic        diagnostic;

            if (PortableExecutableReference.TryGetMetadata(
                    properties.Kind == MetadataImageKind.Assembly,
                    messageProviderOpt,
                    Location.None,
                    display,
                    () => metadataProvider.GetReference(resolvedFullPath, properties),
                    out reference,
                    out diagnostic))
            {
                return(reference);
            }

            diagnosticsOpt.Add(((DiagnosticWithInfo)diagnostic).Info);
            return(new UnresolvedMetadataReference(resolvedFullPath, properties));
        }
예제 #2
0
        /// <summary>
        /// Creates or gets metadata for PE reference.
        /// </summary>
        /// <remarks>
        /// If any of the following exceptions: <see cref="BadImageFormatException"/>, <see cref="FileNotFoundException"/>, <see cref="IOException"/>,
        /// are thrown while reading the metadata file, the exception is caught and an appropriate diagnostic stored in <paramref name="diagnostics"/>.
        /// </remarks>
        private Metadata GetMetadata(PortableExecutableReference peReference, CommonMessageProvider messageProvider, Location location, DiagnosticBag diagnostics)
        {
            Metadata existingMetadata;

            lock (ObservedMetadata)
            {
                if (TryGetObservedMetadata(peReference, diagnostics, out existingMetadata))
                {
                    return(existingMetadata);
                }
            }

            Metadata   newMetadata;
            Diagnostic newDiagnostic;

            PortableExecutableReference.TryGetMetadata(
                peReference.Properties.Kind == MetadataImageKind.Assembly,
                messageProvider,
                location,
                peReference.Display,
                peReference.GetMetadata,
                out newMetadata,
                out newDiagnostic);

            lock (ObservedMetadata)
            {
                if (TryGetObservedMetadata(peReference, diagnostics, out existingMetadata))
                {
                    return(existingMetadata);
                }

                if (newDiagnostic != null)
                {
                    diagnostics.Add(newDiagnostic);
                }

                ObservedMetadata.Add(peReference, (MetadataOrDiagnostic)newMetadata ?? newDiagnostic);
                return(newMetadata);
            }
        }