// // 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); }
// // 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)); } } }
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 static Type GetTypeFromHandle(RuntimeTypeHandle handle) => ReflectionCoreNonPortable.GetTypeForRuntimeTypeHandle(handle);
internal static RuntimeType ToRuntimeType(this RuntimeTypeHandle runtimeTypeHandle) { return(ReflectionCoreNonPortable.GetTypeForRuntimeTypeHandle(runtimeTypeHandle)); }
// // 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. }