Example #1
0
        /// <summary>
        /// Defines a new .NET module.
        /// </summary>
        /// <param name="name">The name of the module.</param>
        /// <param name="corLib">The reference to the common object runtime (COR) library that this module will use.</param>
        public ModuleDefinition(string name, AssemblyReference corLib)
            : this(new MetadataToken(TableIndex.Module, 0))
        {
            Name = name;

            var importer = new ReferenceImporter(this);

            corLib = (AssemblyReference)importer.ImportScope(corLib);

            CorLibTypeFactory = new CorLibTypeFactory(corLib);
            AssemblyReferences.Add(corLib);

            OriginalTargetRuntime = DetectTargetRuntime();
            MetadataResolver      = new DefaultMetadataResolver(CreateAssemblyResolver());

            TopLevelTypes.Add(new TypeDefinition(null, "<Module>", 0));
        }
Example #2
0
        /// <summary>
        /// Imports a resolution scope.
        /// </summary>
        /// <param name="scope">The resolution scope to import.</param>
        /// <returns>The imported resolution scope.</returns>
        public IResolutionScope ImportScope(IResolutionScope scope)
        {
            if (scope is null)
            {
                throw new ArgumentNullException(nameof(scope));
            }
            if (scope.Module == TargetModule)
            {
                return(scope);
            }

            return(scope switch
            {
                AssemblyReference assembly => ImportAssembly(assembly),
                TypeReference parentType => (IResolutionScope)ImportType(parentType),
                ModuleDefinition moduleDef => ImportAssembly(moduleDef.Assembly),
                ModuleReference moduleRef => ImportModule(moduleRef),
                _ => throw new ArgumentOutOfRangeException(nameof(scope))
            });