/// <summary>
        /// Creates a wrapper given a handle.
        /// </summary>
        /// <param name="entity">The handle of the element to be wrapped.</param>
        /// <param name="assemblyMetadata">The module hosting the handle.</param>
        /// <returns>A wrapper or null if one cannot be created.</returns>
        public static IHandleTypeNamedWrapper?Create(EntityHandle entity, AssemblyMetadata assemblyMetadata)
        {
            if (entity.IsNil)
            {
                return(null);
            }

            switch (entity.Kind)
            {
            case HandleKind.EventDefinition:
                return(EventWrapper.Create((EventDefinitionHandle)entity, assemblyMetadata));

            case HandleKind.FieldDefinition:
                return(FieldWrapper.Create((FieldDefinitionHandle)entity, assemblyMetadata));

            case HandleKind.MethodDefinition:
                return(MethodWrapper.Create((MethodDefinitionHandle)entity, assemblyMetadata));

            case HandleKind.PropertyDefinition:
                return(PropertyWrapper.Create((PropertyDefinitionHandle)entity, assemblyMetadata));

            case HandleKind.TypeDefinition:
                return(TypeWrapper.Create((TypeDefinitionHandle)entity, assemblyMetadata));

            case HandleKind.MemberReference:
                return(MemberReferenceWrapper.Create((MemberReferenceHandle)entity, assemblyMetadata));

            case HandleKind.TypeSpecification:
                var specification = TypeSpecificationWrapper.Create((TypeSpecificationHandle)entity, assemblyMetadata);
                return(specification?.Type);

            case HandleKind.InterfaceImplementation:
                return(InterfaceImplementationWrapper.Create((InterfaceImplementationHandle)entity, assemblyMetadata));

            case HandleKind.TypeReference:
                var typeWrapper = TypeReferenceWrapper.Create((TypeReferenceHandle)entity, assemblyMetadata);
                return(typeWrapper?.Type);
            }

            return(null);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="AssemblyMetadata"/> class.
        /// </summary>
        /// <param name="fileName">The file name to the module.</param>
        /// <param name="metadataRepository">The MetadataRepository unit that holds the module.</param>
        /// <param name="typeProvider">A type provider for decoding signatures.</param>
        internal AssemblyMetadata(string fileName, MetadataRepository metadataRepository, TypeProvider typeProvider)
        {
            FileName           = fileName ?? throw new ArgumentNullException(nameof(fileName));
            MetadataRepository = metadataRepository ?? throw new ArgumentNullException(nameof(metadataRepository));
            TypeProvider       = typeProvider;

            _reader        = new PEReader(new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read), PEStreamOptions.PrefetchMetadata);
            MetadataReader = _reader.GetMetadataReader();

            _types                 = new Lazy <IReadOnlyList <TypeWrapper> >(() => TypeWrapper.CreateChecked(MetadataReader.TypeDefinitions, this), LazyThreadSafetyMode.PublicationOnly);
            _namesToTypes          = new Lazy <IReadOnlyDictionary <string, TypeWrapper> >(() => Types.ToDictionary(x => x.FullName), LazyThreadSafetyMode.PublicationOnly);
            _typeReferences        = new Lazy <IReadOnlyList <TypeReferenceWrapper> >(() => TypeReferenceWrapper.CreateChecked(MetadataReader.TypeReferences, this), LazyThreadSafetyMode.PublicationOnly);
            _assemblyReferences    = new Lazy <IReadOnlyList <AssemblyReferenceWrapper> >(() => AssemblyReferenceWrapper.CreateChecked(MetadataReader.AssemblyReferences, this), LazyThreadSafetyMode.PublicationOnly);
            _methodSemanticsLookup = new Lazy <MethodSemanticsLookup>(() => new MethodSemanticsLookup(MetadataReader), LazyThreadSafetyMode.PublicationOnly);
            _mainAssembly          = new Lazy <AssemblyWrapper>(() => new AssemblyWrapper(this), LazyThreadSafetyMode.PublicationOnly);
            _moduleDefinition      = new Lazy <ModuleDefinitionWrapper>(() => ModuleDefinitionWrapper.Create(MetadataReader.GetModuleDefinition(), this), LazyThreadSafetyMode.PublicationOnly);
        }