Esempio n. 1
0
        public sealed override Assembly Load(string assemblyPath)
        {
            if (assemblyPath == null)
                throw new ArgumentNullException(nameof(assemblyPath));

            return RuntimeAssemblyInfo.GetRuntimeAssemblyFromPath(assemblyPath);
        }
Esempio n. 2
0
        public sealed override Assembly Load(byte[] rawAssembly, byte[] pdbSymbolStore)
        {
            if (rawAssembly == null)
                throw new ArgumentNullException(nameof(rawAssembly));

            return RuntimeAssemblyInfo.GetRuntimeAssemblyFromByteArray(rawAssembly, pdbSymbolStore);
        }
        public sealed override Assembly Load(ReadOnlySpan <byte> rawAssembly, ReadOnlySpan <byte> pdbSymbolStore)
        {
            if (rawAssembly.IsEmpty)
            {
                throw new ArgumentNullException(nameof(rawAssembly));
            }

            return(RuntimeAssemblyInfo.GetRuntimeAssemblyFromByteArray(rawAssembly, pdbSymbolStore));
        }
Esempio n. 4
0
 public sealed override Assembly Load(AssemblyName assemblyRef, bool throwOnFileNotFound)
 {
     if (assemblyRef == null)
         throw new ArgumentNullException(nameof(assemblyRef));
     if (throwOnFileNotFound)
         return RuntimeAssemblyInfo.GetRuntimeAssembly(assemblyRef.ToRuntimeAssemblyName());
     else
         return RuntimeAssemblyInfo.GetRuntimeAssemblyIfExists(assemblyRef.ToRuntimeAssemblyName());
 }
        internal sealed override RuntimeTypeInfo UncachedGetTypeCoreCaseSensitive(string fullName)
        {
            string[] parts             = fullName.Split('.');
            int      numNamespaceParts = parts.Length - 1;

            string[] namespaceParts = new string[numNamespaceParts];
            for (int i = 0; i < numNamespaceParts; i++)
            {
                namespaceParts[numNamespaceParts - i - 1] = parts[i];
            }
            string name = parts[numNamespaceParts];

            foreach (QScopeDefinition scopeDefinition in AllScopes)
            {
                MetadataReader        reader = scopeDefinition.Reader;
                ScopeDefinitionHandle scopeDefinitionHandle = scopeDefinition.Handle;

                NamespaceDefinition namespaceDefinition;
                if (!TryResolveNamespaceDefinitionCaseSensitive(reader, namespaceParts, scopeDefinitionHandle, out namespaceDefinition))
                {
                    continue;
                }

                // We've successfully drilled down the namespace chain. Now look for a top-level type matching the type name.
                TypeDefinitionHandleCollection candidateTypes = namespaceDefinition.TypeDefinitions;
                foreach (TypeDefinitionHandle candidateType in candidateTypes)
                {
                    TypeDefinition typeDefinition = candidateType.GetTypeDefinition(reader);
                    if (typeDefinition.Name.StringEquals(name, reader))
                    {
                        return(candidateType.ResolveTypeDefinition(reader));
                    }
                }

                // No match found in this assembly - see if there's a matching type forwarder.
                TypeForwarderHandleCollection candidateTypeForwarders = namespaceDefinition.TypeForwarders;
                foreach (TypeForwarderHandle typeForwarderHandle in candidateTypeForwarders)
                {
                    TypeForwarder typeForwarder = typeForwarderHandle.GetTypeForwarder(reader);
                    if (typeForwarder.Name.StringEquals(name, reader))
                    {
                        RuntimeAssemblyName redirectedAssemblyName = typeForwarder.Scope.ToRuntimeAssemblyName(reader);
                        RuntimeAssemblyInfo redirectedAssembly     = RuntimeAssemblyInfo.GetRuntimeAssemblyIfExists(redirectedAssemblyName);
                        if (redirectedAssembly == null)
                        {
                            return(null);
                        }
                        return(redirectedAssembly.GetTypeCoreCaseSensitive(fullName));
                    }
                }
            }

            return(null);
        }
Esempio n. 6
0
        private static CoreTypeResolver CreateCoreTypeResolver(Func <Assembly, string, bool, Type> typeResolver, IList <string> defaultAssemblyNames, bool throwOnError, bool ignoreCase)
        {
            if (typeResolver == null)
            {
                return(delegate(Assembly containingAssemblyIfAny, string coreTypeName)
                {
                    if (containingAssemblyIfAny != null)
                    {
                        return containingAssemblyIfAny.GetTypeCore(coreTypeName, ignoreCase: ignoreCase);
                    }
                    else
                    {
                        foreach (string defaultAssemblyName in defaultAssemblyNames)
                        {
                            RuntimeAssemblyName runtimeAssemblyName = RuntimeAssemblyName.Parse(defaultAssemblyName);
                            RuntimeAssemblyInfo defaultAssembly = RuntimeAssemblyInfo.GetRuntimeAssemblyIfExists(runtimeAssemblyName);
                            if (defaultAssembly == null)
                            {
                                continue;
                            }
                            Type resolvedType = defaultAssembly.GetTypeCore(coreTypeName, ignoreCase: ignoreCase);
                            if (resolvedType != null)
                            {
                                return resolvedType;
                            }
                        }

                        if (throwOnError && defaultAssemblyNames.Count > 0)
                        {
                            // Though we don't have to throw a TypeLoadException exception (that's our caller's job), we can throw a more specific exception than he would so just do it.
                            throw Helpers.CreateTypeLoadException(coreTypeName, defaultAssemblyNames[0]);
                        }
                        return null;
                    }
                });
            }
            else
            {
                return(delegate(Assembly containingAssemblyIfAny, string coreTypeName)
                {
                    string escapedName = coreTypeName.EscapeTypeNameIdentifier();
                    Type type = typeResolver(containingAssemblyIfAny, escapedName, ignoreCase);
                    return type;
                });
            }
        }
        internal sealed override RuntimeTypeInfo GetTypeCoreCaseInsensitive(string fullName)
        {
            LowLevelDictionary <string, QHandle> dict = CaseInsensitiveTypeDictionary;
            QHandle qualifiedHandle;

            if (!dict.TryGetValue(fullName.ToLowerInvariant(), out qualifiedHandle))
            {
                return(null);
            }

            MetadataReader reader = qualifiedHandle.Reader;
            Handle         typeDefOrForwarderHandle = qualifiedHandle.Handle;

            HandleType handleType = typeDefOrForwarderHandle.HandleType;

            switch (handleType)
            {
            case HandleType.TypeDefinition:
            {
                TypeDefinitionHandle typeDefinitionHandle = typeDefOrForwarderHandle.ToTypeDefinitionHandle(reader);
                return(typeDefinitionHandle.ResolveTypeDefinition(reader));
            }

            case HandleType.TypeForwarder:
            {
                TypeForwarder        typeForwarder           = typeDefOrForwarderHandle.ToTypeForwarderHandle(reader).GetTypeForwarder(reader);
                ScopeReferenceHandle destinationScope        = typeForwarder.Scope;
                RuntimeAssemblyName  destinationAssemblyName = destinationScope.ToRuntimeAssemblyName(reader);
                RuntimeAssemblyInfo  destinationAssembly     = RuntimeAssemblyInfo.GetRuntimeAssemblyIfExists(destinationAssemblyName);
                if (destinationAssembly == null)
                {
                    return(null);
                }
                return(destinationAssembly.GetTypeCoreCaseInsensitive(fullName));
            }

            default:
                throw new InvalidOperationException();
            }
        }
Esempio n. 8
0
        private static RuntimeTypeInfo?TryResolveTypeReference(this TypeReferenceHandle typeReferenceHandle, MetadataReader reader, ref Exception?exception)
        {
            RuntimeTypeHandle resolvedRuntimeTypeHandle;

            if (ReflectionCoreExecution.ExecutionEnvironment.TryGetNamedTypeForTypeReference(reader, typeReferenceHandle, out resolvedRuntimeTypeHandle))
            {
                return(resolvedRuntimeTypeHandle.GetTypeForRuntimeTypeHandle());
            }

            TypeReference typeReference = typeReferenceHandle.GetTypeReference(reader);
            string        name          = typeReference.TypeName.GetString(reader);
            Handle        parent        = typeReference.ParentNamespaceOrType;
            HandleType    parentType    = parent.HandleType;
            TypeInfo?     outerTypeInfo = null;

            // Check if this is a reference to a nested type.

            if (parentType == HandleType.TypeDefinition)
            {
                outerTypeInfo = parent.ToTypeDefinitionHandle(reader).GetNamedType(reader);
            }
            else if (parentType == HandleType.TypeReference)
            {
                RuntimeTypeInfo?outerType = parent.ToTypeReferenceHandle(reader).TryResolveTypeReference(reader, ref exception);
                if (outerType == null)
                {
                    return(null);
                }
                outerTypeInfo = outerType;   // Since we got to outerType via a metadata reference, we're assured GetTypeInfo() won't throw a MissingMetadataException.
            }
            if (outerTypeInfo != null)
            {
                // It was a nested type. We've already resolved the containing type recursively - just find the nested among its direct children.
                TypeInfo?resolvedTypeInfo = outerTypeInfo.GetDeclaredNestedType(name);
                if (resolvedTypeInfo == null)
                {
                    exception = ReflectionCoreExecution.ExecutionDomain.CreateMissingMetadataException(outerTypeInfo, name);
                    return(null);
                }
                return(resolvedTypeInfo.CastToRuntimeTypeInfo());
            }


            // If we got here, the typeReference was to a non-nested type.
            if (parentType == HandleType.NamespaceReference)
            {
                NamespaceReferenceHandle namespaceReferenceHandle = parent.ToNamespaceReferenceHandle(reader);
                string fullName             = namespaceReferenceHandle.ToFullyQualifiedTypeName(name, reader);
                Handle parentHandleToSearch = parent;

                while (parentHandleToSearch.HandleType != HandleType.ScopeReference)
                {
                    parentHandleToSearch = parentHandleToSearch.ToNamespaceReferenceHandle(reader).GetNamespaceReference(reader).ParentScopeOrNamespace;
                }
                ScopeReferenceHandle scopeReferenceHandle = parentHandleToSearch.ToScopeReferenceHandle(reader);

                RuntimeAssemblyName assemblyName = scopeReferenceHandle.ToRuntimeAssemblyName(reader);
                RuntimeAssemblyInfo runtimeAssembly;
                exception = RuntimeAssemblyInfo.TryGetRuntimeAssembly(assemblyName, out runtimeAssembly);
                if (exception != null)
                {
                    return(null);
                }
                RuntimeTypeInfo runtimeType = runtimeAssembly.GetTypeCore(fullName, ignoreCase: false);
                if (runtimeType == null)
                {
                    exception = Helpers.CreateTypeLoadException(fullName, assemblyName.FullName);
                    return(null);
                }
                return(runtimeType);
            }

            throw new BadImageFormatException(); // Expected TypeReference parent to be typeRef, typeDef or namespaceRef.
        }
Esempio n. 9
0
 public sealed override Assembly[] GetLoadedAssemblies() => RuntimeAssemblyInfo.GetLoadedAssemblies();
Esempio n. 10
0
        public sealed override void RunModuleConstructor(Module module)
        {
            RuntimeAssemblyInfo assembly = (RuntimeAssemblyInfo)module.Assembly;

            assembly.RunModuleConstructor();
        }