/// <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);
        }
예제 #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MetadataRepository"/> class.
        /// </summary>
        /// <param name="mainModulePath">The path to the main module.</param>
        /// <param name="searchDirectories">The directories to search for additional dependencies.</param>
        public MetadataRepository(string mainModulePath, IEnumerable <string> searchDirectories)
        {
            if (searchDirectories == null)
            {
                throw new ArgumentNullException(nameof(searchDirectories));
            }

            if (mainModulePath == null)
            {
                throw new ArgumentNullException(nameof(mainModulePath));
            }

            TypeProvider         = new TypeProvider(this);
            MainAssemblyMetadata = new AssemblyMetadata(mainModulePath, this, TypeProvider);
            SearchDirectories    = searchDirectories.ToList();

            RootNamespace = new NamespaceWrapper(MainAssemblyMetadata.MetadataReader.GetNamespaceDefinitionRoot(), MainAssemblyMetadata);

            _referenceAssemblies = new Lazy <IReadOnlyList <AssemblyMetadata> >(GetReferenceAssemblies, LazyThreadSafetyMode.PublicationOnly);
        }