src/Microsoft/Windows/ComponentObjectModel/IO/_Library/Metadata/ComponentLibraryMetadataReader.cs
/// <summary>
/// Reads COM component information from a component library
/// </summary>
/// <param name="path">Full qualified path to the component library file</param>
/// <returns>A <see cref="ComponentLibraryMetadata"/> instance</returns>
/// <remarks>
/// The method also accepts the path to a .NET Core ComHost file. ([file].comhost.dll)
/// In this case, the path to the .NET core assembly gets calculated from the comhost path and the metadata gets read from the assembly.
/// </remarks>
Reads COM component information from a component library
Reads COM component information from a component library
public ComponentLibraryMetadata Read( string path ) {
            FileAttributes fileAttributes = File.GetAttributes(path);
            if ( (fileAttributes & FileAttributes.Directory) == FileAttributes.Directory ) {
                throw new NotSupportedException();
            }
            string libraryPath = Utils.GetLibraryPath(path);
            if ( !File.Exists( libraryPath ) ) {
                throw new FileNotFoundException();
            }
            ComponentLibraryMetadata result = new ComponentLibraryMetadata() {
                Path = libraryPath,
            };
            try {
                result.AssemblyName = AssemblyName.GetAssemblyName( libraryPath );
                result.ImageType = ComponentLibraryImageType.Managed;
            }
            catch ( BadImageFormatException ) {
                throw new NotImplementedException();
            }
            using ( Stream stream = File.OpenRead( libraryPath ) ) {
                using ( PEReader reader = new PEReader( stream ) ) {
                    MetadataReader metadataReader = reader.GetMetadataReader();
                    result.AssemblyRuntimeVersion = metadataReader.MetadataVersion;
                    result.Attributes = ReadCustomAttributes( metadataReader, metadataReader.CustomAttributes, HandleKind.AssemblyDefinition );
                    if ( true == result.Attributes.TryGet( out TargetFrameworkAttribute targetFrameworkAttribute ) ) {
                        FrameworkName frameworkName = new FrameworkName(targetFrameworkAttribute.FrameworkName);
                        result.AssemblyTargetFrameworkVersion = frameworkName.Version;
                        switch ( frameworkName.Identifier ) {
                            case ".NETFramework": {
                                result.AssemblyTargetFramework = ComponentLibraryTargetFramework.DotNetFramework;
                                break;
                            }
                            case ".NETCoreApp": {
                                result.AssemblyTargetFramework = ComponentLibraryTargetFramework.DotNetCore;
                                break;
                            }
                            default: {
                                throw new ArgumentOutOfRangeException();
                            }
                        }
                    }
                    ReadTypes( metadataReader, result );
                }
            }
            return result;
        }
public ComponentLibraryMetadata Read ( string path ) { FileAttributes fileAttributes = File . GetAttributes ( path ) ; if ( ( fileAttributes & FileAttributes . Directory ) == FileAttributes . Directory ) { throw new NotSupportedException ( ) ; } string libraryPath = Utils . GetLibraryPath ( path ) ; if ( ! File . Exists ( libraryPath ) ) { throw new FileNotFoundException ( ) ; } ComponentLibraryMetadata result = new ComponentLibraryMetadata ( ) { Path = libraryPath , } ; try { result . AssemblyName = AssemblyName . GetAssemblyName ( libraryPath ) ; result . ImageType = ComponentLibraryImageType . Managed ; } catch ( BadImageFormatException ) { throw new NotImplementedException ( ) ; } using ( Stream stream = File . OpenRead ( libraryPath ) ) { using ( PEReader reader = new PEReader ( stream ) ) { MetadataReader metadataReader = reader . GetMetadataReader ( ) ; result . AssemblyRuntimeVersion = metadataReader . MetadataVersion ; result . Attributes = ReadCustomAttributes ( metadataReader , metadataReader . CustomAttributes , HandleKind . AssemblyDefinition ) ; if ( true == result . Attributes . TryGet ( out TargetFrameworkAttribute targetFrameworkAttribute ) ) { FrameworkName frameworkName = new FrameworkName ( targetFrameworkAttribute . FrameworkName ) ; result . AssemblyTargetFrameworkVersion = frameworkName . Version ; switch ( frameworkName . Identifier ) { case " " : { result . AssemblyTargetFramework = ComponentLibraryTargetFramework . DotNetFramework ; break ; } case " " : { result . AssemblyTargetFramework = ComponentLibraryTargetFramework . DotNetCore ; break ; } default : { throw new ArgumentOutOfRangeException ( ) ; } } } ReadTypes ( metadataReader , result ) ; } } return result ; }
Reads COM component information from a component library
Reads COM component information from a component library
// Method does not scan directories // Process assembly information // Build list of referenced assemblies. Currently not needed, so not uncommented //foreach ( AssemblyReferenceHandle referenceHandle in metadataReader.AssemblyReferences ) { //    AssemblyReference assemblyReference = metadataReader.GetAssemblyReference(referenceHandle); //    AssemblyName      assemblyName      = assemblyReference.GetAssemblyName(); //}
None
path
string
Full qualified path to the component library file
Full qualified path to the component library file
remarks
The method also accepts the path to a .NET Core ComHost file. ([file].comhost.dll)
In this case, the path to the .NET core assembly gets calculated from the comhost path and the metadata gets read from the assembly.
The method also accepts the path to a . NET Core ComHost file . ( [ file ] . comhost . dll ) In this case the path to the . NET core assembly gets calculated from the comhost path and the metadata gets read from the assembly .