public sealed override Exception TryResolve(ReflectionDomain reflectionDomain, RuntimeAssembly currentAssembly, bool ignoreCase, out RuntimeType result) { result = null; RuntimeType genericType; Exception typeLoadException = GenericType.TryResolve(reflectionDomain, currentAssembly, ignoreCase, out genericType); if (typeLoadException != null) { return(typeLoadException); } LowLevelList <RuntimeType> genericTypeArguments = new LowLevelList <RuntimeType>(); foreach (TypeName genericTypeArgumentName in GenericArguments) { RuntimeType genericTypeArgument; typeLoadException = genericTypeArgumentName.TryResolve(reflectionDomain, currentAssembly, ignoreCase, out genericTypeArgument); if (typeLoadException != null) { return(typeLoadException); } genericTypeArguments.Add(genericTypeArgument); } result = ReflectionCoreNonPortable.GetConstructedGenericType(genericType, genericTypeArguments.ToArray()); return(null); }
// // Retrieves the MethodBase for a given method handle. Helper to implement Delegate.GetMethodInfo() // public MethodBase GetMethod(RuntimeTypeHandle declaringTypeHandle, MethodHandle methodHandle, RuntimeTypeHandle[] genericMethodTypeArgumentHandles) { RuntimeType declaringType = ReflectionCoreNonPortable.GetTypeForRuntimeTypeHandle(declaringTypeHandle); RuntimeTypeInfo contextTypeInfo = declaringType.GetRuntimeTypeInfo(); RuntimeNamedTypeInfo definingTypeInfo = contextTypeInfo.AnchoringTypeDefinitionForDeclaredMembers; MetadataReader reader = definingTypeInfo.Reader; if (methodHandle.IsConstructor(reader)) { return(RuntimePlainConstructorInfo.GetRuntimePlainConstructorInfo(methodHandle, definingTypeInfo, contextTypeInfo)); } else { RuntimeNamedMethodInfo runtimeNamedMethodInfo = RuntimeNamedMethodInfo.GetRuntimeNamedMethodInfo(methodHandle, definingTypeInfo, contextTypeInfo); if (!runtimeNamedMethodInfo.IsGenericMethod) { return(runtimeNamedMethodInfo); } else { RuntimeType[] genericTypeArguments = new RuntimeType[genericMethodTypeArgumentHandles.Length]; for (int i = 0; i < genericMethodTypeArgumentHandles.Length; i++) { genericTypeArguments[i] = ReflectionCoreNonPortable.GetTypeForRuntimeTypeHandle(genericMethodTypeArgumentHandles[i]); } return(RuntimeConstructedGenericMethodInfo.GetRuntimeConstructedGenericMethodInfo(runtimeNamedMethodInfo, genericTypeArguments)); } } }
// // This overload of GetMethodHandle can handle all method handles. // public sealed override MethodBase GetMethodFromHandle(RuntimeMethodHandle runtimeMethodHandle, RuntimeTypeHandle declaringTypeHandle) { ExecutionEnvironment executionEnvironment = ReflectionCoreExecution.ExecutionEnvironment; MethodHandle methodHandle; RuntimeTypeHandle[] genericMethodTypeArgumentHandles; if (!executionEnvironment.TryGetMethodFromHandleAndType(runtimeMethodHandle, declaringTypeHandle, out methodHandle, out genericMethodTypeArgumentHandles)) { // This may be a method declared on a non-generic type: this api accepts that too so try the other table. RuntimeTypeHandle actualDeclaringTypeHandle; if (!executionEnvironment.TryGetMethodFromHandle(runtimeMethodHandle, out actualDeclaringTypeHandle, out methodHandle, out genericMethodTypeArgumentHandles)) { throw new ArgumentException(SR.Argument_InvalidHandle); } if (!actualDeclaringTypeHandle.Equals(declaringTypeHandle)) { throw new ArgumentException(SR.Format(SR.Argument_ResolveMethodHandle, ReflectionCoreNonPortable.GetTypeForRuntimeTypeHandle(declaringTypeHandle), ReflectionCoreNonPortable.GetTypeForRuntimeTypeHandle(actualDeclaringTypeHandle))); } } MethodBase methodBase = ReflectionCoreExecution.ExecutionDomain.GetMethod(declaringTypeHandle, methodHandle, genericMethodTypeArgumentHandles); return(methodBase); }
// // This overload of GetFieldHandle can handle all field handles. // public sealed override FieldInfo GetFieldFromHandle(RuntimeFieldHandle runtimeFieldHandle, RuntimeTypeHandle declaringTypeHandle) { ExecutionEnvironment executionEnvironment = ReflectionCoreExecution.ExecutionEnvironment; FieldHandle fieldHandle; if (!executionEnvironment.TryGetFieldFromHandleAndType(runtimeFieldHandle, declaringTypeHandle, out fieldHandle)) { // This may be a field declared on a non-generic type: this api accepts that too so try the other table. RuntimeTypeHandle actualDeclaringTypeHandle; if (!executionEnvironment.TryGetFieldFromHandle(runtimeFieldHandle, out actualDeclaringTypeHandle, out fieldHandle)) { throw new ArgumentException(SR.Argument_InvalidHandle); } if (!actualDeclaringTypeHandle.Equals(declaringTypeHandle)) { throw new ArgumentException(SR.Format(SR.Argument_ResolveFieldHandle, ReflectionCoreNonPortable.GetTypeForRuntimeTypeHandle(declaringTypeHandle), ReflectionCoreNonPortable.GetTypeForRuntimeTypeHandle(actualDeclaringTypeHandle))); } } FieldInfo fieldInfo = GetFieldInfo(declaringTypeHandle, fieldHandle); return(fieldInfo); }
private static RuntimeTypeHandle GetRuntimeTypeHandle(IntPtr pEEType) { #if CLR_RUNTIMETYPEHANDLE return(new RuntimeTypeHandle(ReflectionCoreNonPortable.GetRuntimeTypeForEEType(new EETypePtr(pEEType)))); #else return(new RuntimeTypeHandle(new EETypePtr(pEEType))); #endif }
private FieldInfo GetFieldInfo(RuntimeTypeHandle declaringTypeHandle, FieldHandle fieldHandle) { RuntimeType declaringType = ReflectionCoreNonPortable.GetTypeForRuntimeTypeHandle(declaringTypeHandle); RuntimeTypeInfo contextTypeInfo = declaringType.GetRuntimeTypeInfo(); RuntimeNamedTypeInfo definingTypeInfo = contextTypeInfo.AnchoringTypeDefinitionForDeclaredMembers; MetadataReader reader = definingTypeInfo.Reader; return RuntimeFieldInfo.GetRuntimeFieldInfo(fieldHandle, definingTypeInfo, contextTypeInfo); }
protected sealed override RuntimeType Factory(QTypeDefinition qTypeDefinition) { RuntimeTypeHandle runtimeTypeHandle; if (ReflectionCoreExecution.ExecutionEnvironment.TryGetNamedTypeForMetadata(qTypeDefinition.Reader, qTypeDefinition.Handle, out runtimeTypeHandle)) { return(ReflectionCoreNonPortable.GetTypeForRuntimeTypeHandle(runtimeTypeHandle)); } else { return(RuntimeInspectionOnlyNamedType.GetRuntimeInspectionOnlyNamedType(qTypeDefinition.Reader, qTypeDefinition.Handle)); } }
public sealed override Exception TryResolve(ReflectionDomain reflectionDomain, RuntimeAssembly currentAssembly, bool ignoreCase, out RuntimeType result) { result = null; RuntimeType elementType; Exception typeLoadException = ElementTypeName.TryResolve(reflectionDomain, currentAssembly, ignoreCase, out elementType); if (typeLoadException != null) { return(typeLoadException); } result = ReflectionCoreNonPortable.GetPointerType(elementType); return(null); }
private Exception UncachedTryResolveCaseSensitive(ReflectionDomain reflectionDomain, RuntimeAssembly currentAssembly, out RuntimeType result) { result = null; foreach (QScopeDefinition scopeDefinition in currentAssembly.AllScopes) { MetadataReader reader = scopeDefinition.Reader; ScopeDefinitionHandle scopeDefinitionHandle = scopeDefinition.Handle; NamespaceDefinition namespaceDefinition; if (!TryResolveNamespaceDefinitionCaseSensitive(reader, scopeDefinitionHandle, out namespaceDefinition)) { continue; } // We've successfully drilled down the namespace chain. Now look for a top-level type matching the type name. IEnumerable <TypeDefinitionHandle> candidateTypes = namespaceDefinition.TypeDefinitions; foreach (TypeDefinitionHandle candidateType in candidateTypes) { TypeDefinition typeDefinition = candidateType.GetTypeDefinition(reader); if (typeDefinition.Name.StringEquals(_name, reader)) { result = reflectionDomain.ResolveTypeDefinition(reader, candidateType); return(null); } } // No match found in this assembly - see if there's a matching type forwarder. IEnumerable <TypeForwarderHandle> 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); AssemblyQualifiedTypeName redirectedTypeName = new AssemblyQualifiedTypeName(this, redirectedAssemblyName); return(redirectedTypeName.TryResolve(reflectionDomain, null, /*ignoreCase: */ false, out result)); } } } { String typeName = this.ToString(); String message = SR.Format(SR.TypeLoad_TypeNotFound, typeName, currentAssembly.FullName); return(ReflectionCoreNonPortable.CreateTypeLoadException(message, typeName)); } }
public static TypeLoadException CreateTypeLoadException(string typeName, string assemblyName) { string message = SR.Format(SR.TypeLoad_TypeNotFoundInAssembly, typeName, assemblyName); return(ReflectionCoreNonPortable.CreateTypeLoadException(message, typeName)); }
public Type GetType() { return(ReflectionCoreNonPortable.GetRuntimeTypeForEEType(EETypePtr)); }
// Special coersion rules for primitives, enums and pointer. private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(object srcObject, EETypePtr srcEEType, EETypePtr dstEEType, CheckArgumentSemantics semantics, out object dstObject) { if (semantics == CheckArgumentSemantics.SetFieldDirect && (srcEEType.IsEnum || dstEEType.IsEnum)) { dstObject = null; return(CreateChangeTypeException(srcEEType, dstEEType, semantics)); } if (dstEEType.IsPointer) { Exception exception = ConvertPointerIfPossible(srcObject, srcEEType, dstEEType, semantics, out IntPtr dstIntPtr); if (exception != null) { dstObject = null; return(exception); } dstObject = dstIntPtr; return(null); } if (!(srcEEType.IsPrimitive && dstEEType.IsPrimitive)) { dstObject = null; return(CreateChangeTypeException(srcEEType, dstEEType, semantics)); } RuntimeImports.RhCorElementType dstCorElementType = dstEEType.CorElementType; if (!srcEEType.CorElementTypeInfo.CanWidenTo(dstCorElementType)) { dstObject = null; return(CreateChangeTypeArgumentException(srcEEType, dstEEType)); } switch (dstCorElementType) { case RuntimeImports.RhCorElementType.ELEMENT_TYPE_BOOLEAN: dstObject = Convert.ToBoolean(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_CHAR: dstObject = Convert.ToChar(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I1: dstObject = Convert.ToSByte(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I2: dstObject = Convert.ToInt16(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I4: dstObject = Convert.ToInt32(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I8: dstObject = Convert.ToInt64(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U1: dstObject = Convert.ToByte(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U2: dstObject = Convert.ToUInt16(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U4: dstObject = Convert.ToUInt32(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U8: dstObject = Convert.ToUInt64(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_R4: if (srcEEType.CorElementType == RuntimeImports.RhCorElementType.ELEMENT_TYPE_CHAR) { dstObject = (float)(char)srcObject; } else { dstObject = Convert.ToSingle(srcObject); } break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_R8: if (srcEEType.CorElementType == RuntimeImports.RhCorElementType.ELEMENT_TYPE_CHAR) { dstObject = (double)(char)srcObject; } else { dstObject = Convert.ToDouble(srcObject); } break; default: Debug.Assert(false, "Unexpected CorElementType: " + dstCorElementType + ": Not a valid widening target."); dstObject = null; return(CreateChangeTypeException(srcEEType, dstEEType, semantics)); } if (dstEEType.IsEnum) { Type dstType = ReflectionCoreNonPortable.GetRuntimeTypeForEEType(dstEEType); dstObject = Enum.ToObject(dstType, dstObject); } Debug.Assert(dstObject.EETypePtr == dstEEType); return(null); }
public static Type GetTypeFromHandle(RuntimeTypeHandle handle) => ReflectionCoreNonPortable.GetTypeForRuntimeTypeHandle(handle);
internal static RuntimeType ToRuntimeType(this RuntimeTypeHandle runtimeTypeHandle) { return(ReflectionCoreNonPortable.GetTypeForRuntimeTypeHandle(runtimeTypeHandle)); }
private static RuntimeTypeHandle GetRuntimeTypeHandle(IntPtr pEEType) { return(new RuntimeTypeHandle(ReflectionCoreNonPortable.GetRuntimeTypeForEEType(new EETypePtr(pEEType)))); }
internal static Object CheckArgument(Object srcObject, EETypePtr dstEEType, CheckArgumentSemantics semantics) { if (srcObject == null) { // null -> default(T) if (dstEEType.IsValueType && !dstEEType.IsNullable) { return(Runtime.RuntimeImports.RhNewObject(dstEEType)); } else { return(null); } } else { EETypePtr srcEEType = srcObject.EETypePtr; if (RuntimeImports.AreTypesAssignable(srcEEType, dstEEType)) { return(srcObject); } if (dstEEType.IsInterface) { ICastable castable = srcObject as ICastable; Exception castError; if (castable != null && castable.IsInstanceOfInterface(new RuntimeTypeHandle(dstEEType), out castError)) { return(srcObject); } } if (!((srcEEType.IsEnum || srcEEType.IsPrimitive) && (dstEEType.IsEnum || dstEEType.IsPrimitive))) { throw CreateChangeTypeException(srcEEType, dstEEType, semantics); } RuntimeImports.RhCorElementType dstCorElementType = dstEEType.CorElementType; if (!srcEEType.CorElementTypeInfo.CanWidenTo(dstCorElementType)) { throw CreateChangeTypeArgumentException(srcEEType, dstEEType); } Object dstObject; switch (dstCorElementType) { case RuntimeImports.RhCorElementType.ELEMENT_TYPE_BOOLEAN: dstObject = Convert.ToBoolean(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_CHAR: dstObject = Convert.ToChar(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I1: dstObject = Convert.ToSByte(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I2: dstObject = Convert.ToInt16(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I4: dstObject = Convert.ToInt32(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I8: dstObject = Convert.ToInt64(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U1: dstObject = Convert.ToByte(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U2: dstObject = Convert.ToUInt16(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U4: dstObject = Convert.ToUInt32(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U8: dstObject = Convert.ToUInt64(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_R4: if (srcEEType.CorElementType == RuntimeImports.RhCorElementType.ELEMENT_TYPE_CHAR) { dstObject = (float)(char)srcObject; } else { dstObject = Convert.ToSingle(srcObject); } break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_R8: if (srcEEType.CorElementType == RuntimeImports.RhCorElementType.ELEMENT_TYPE_CHAR) { dstObject = (double)(char)srcObject; } else { dstObject = Convert.ToDouble(srcObject); } break; default: Debug.Assert(false, "Unexpected CorElementType: " + dstCorElementType + ": Not a valid widening target."); throw CreateChangeTypeException(srcEEType, dstEEType, semantics); } if (dstEEType.IsEnum) { Type dstType = ReflectionCoreNonPortable.GetRuntimeTypeForEEType(dstEEType); dstObject = Enum.ToObject(dstType, dstObject); } Debug.Assert(dstObject.EETypePtr == dstEEType); return(dstObject); } }
// // Main routine to resolve a typeReference. // private static RuntimeType TryResolveTypeReference(this ReflectionDomain reflectionDomain, MetadataReader reader, TypeReferenceHandle typeReferenceHandle, ref Exception exception) { { ExecutionDomain executionDomain = reflectionDomain as ExecutionDomain; if (executionDomain != null) { RuntimeTypeHandle resolvedRuntimeTypeHandle; if (executionDomain.ExecutionEnvironment.TryGetNamedTypeForTypeReference(reader, typeReferenceHandle, out resolvedRuntimeTypeHandle)) { return(ReflectionCoreNonPortable.GetTypeForRuntimeTypeHandle(resolvedRuntimeTypeHandle)); } } } 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 = RuntimeNamedTypeInfo.GetRuntimeNamedTypeInfo(reader, parent.ToTypeDefinitionHandle(reader)); } else if (parentType == HandleType.TypeReference) { RuntimeType outerType = reflectionDomain.TryResolveTypeReference(reader, parent.ToTypeReferenceHandle(reader), ref exception); if (outerType == null) { return(null); } outerTypeInfo = outerType.GetTypeInfo(); // 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 = reflectionDomain.CreateMissingMetadataException(outerTypeInfo, name); return(null); } return((RuntimeType)(resolvedTypeInfo.AsType())); } // If we got here, the typeReference was to a non-nested type. if (parentType == HandleType.NamespaceReference) { AssemblyQualifiedTypeName assemblyQualifiedTypeName = parent.ToNamespaceReferenceHandle(reader).ToAssemblyQualifiedTypeName(name, reader); RuntimeType runtimeType; exception = assemblyQualifiedTypeName.TryResolve(reflectionDomain, null, /*ignoreCase: */ false, out runtimeType); if (exception != null) { return(null); } return(runtimeType); } throw new BadImageFormatException(); // Expected TypeReference parent to be typeRef, typeDef or namespaceRef. }
// // Main routine to parse a metadata type specification signature. // private static RuntimeType TryResolveTypeSignature(this ReflectionDomain reflectionDomain, MetadataReader reader, TypeSpecificationHandle typeSpecHandle, TypeContext typeContext, ref Exception exception) { Handle typeHandle = typeSpecHandle.GetTypeSpecification(reader).Signature; switch (typeHandle.HandleType) { case HandleType.ArraySignature: { ArraySignature sig = typeHandle.ToArraySignatureHandle(reader).GetArraySignature(reader); int rank = sig.Rank; if (rank <= 0) { throw new BadImageFormatException(); // Bad rank. } RuntimeType elementType = reflectionDomain.TryResolve(reader, sig.ElementType, typeContext, ref exception); if (elementType == null) { return(null); } return(ReflectionCoreNonPortable.GetMultiDimArrayType(elementType, rank)); } case HandleType.ByReferenceSignature: { ByReferenceSignature sig = typeHandle.ToByReferenceSignatureHandle(reader).GetByReferenceSignature(reader); RuntimeType targetType = reflectionDomain.TryResolve(reader, sig.Type, typeContext, ref exception); if (targetType == null) { return(null); } return(ReflectionCoreNonPortable.GetByRefType(targetType)); } case HandleType.MethodTypeVariableSignature: { MethodTypeVariableSignature sig = typeHandle.ToMethodTypeVariableSignatureHandle(reader).GetMethodTypeVariableSignature(reader); return(typeContext.GenericMethodArguments[sig.Number]); } case HandleType.PointerSignature: { PointerSignature sig = typeHandle.ToPointerSignatureHandle(reader).GetPointerSignature(reader); RuntimeType targetType = reflectionDomain.TryResolve(reader, sig.Type, typeContext, ref exception); if (targetType == null) { return(null); } return(ReflectionCoreNonPortable.GetPointerType(targetType)); } case HandleType.SZArraySignature: { SZArraySignature sig = typeHandle.ToSZArraySignatureHandle(reader).GetSZArraySignature(reader); RuntimeType elementType = reflectionDomain.TryResolve(reader, sig.ElementType, typeContext, ref exception); if (elementType == null) { return(null); } return(ReflectionCoreNonPortable.GetArrayType(elementType)); } case HandleType.TypeDefinition: { return(reflectionDomain.ResolveTypeDefinition(reader, typeHandle.ToTypeDefinitionHandle(reader))); } case HandleType.TypeInstantiationSignature: { TypeInstantiationSignature sig = typeHandle.ToTypeInstantiationSignatureHandle(reader).GetTypeInstantiationSignature(reader); RuntimeType genericTypeDefinition = reflectionDomain.TryResolve(reader, sig.GenericType, typeContext, ref exception); if (genericTypeDefinition == null) { return(null); } LowLevelList <RuntimeType> genericTypeArguments = new LowLevelList <RuntimeType>(); foreach (Handle genericTypeArgumentHandle in sig.GenericTypeArguments) { RuntimeType genericTypeArgument = reflectionDomain.TryResolve(reader, genericTypeArgumentHandle, typeContext, ref exception); if (genericTypeArgument == null) { return(null); } genericTypeArguments.Add(genericTypeArgument); } return(ReflectionCoreNonPortable.GetConstructedGenericType(genericTypeDefinition, genericTypeArguments.ToArray())); } case HandleType.TypeReference: { return(reflectionDomain.TryResolveTypeReference(reader, typeHandle.ToTypeReferenceHandle(reader), ref exception)); } case HandleType.TypeVariableSignature: { TypeVariableSignature sig = typeHandle.ToTypeVariableSignatureHandle(reader).GetTypeVariableSignature(reader); return(typeContext.GenericTypeArguments[sig.Number]); } default: throw new NotSupportedException(); // Unexpected Type signature type. } }