public bool SupportsReflection(Type type) { if (!type.IsRuntimeImplemented()) { return(false); } RuntimeTypeInfo runtimeType = type.CastToRuntimeTypeInfo(); if (null == runtimeType.InternalNameIfAvailable) { return(false); } if (ExecutionEnvironment.IsReflectionBlocked(type.TypeHandle)) { // The type is an internal framework type and is blocked from reflection return(false); } if (runtimeType.InternalFullNameOfAssembly == Internal.Runtime.Augments.RuntimeAugments.HiddenScopeAssemblyName) { // The type is an internal framework type but is reflectable for internal class library use // where we make the type appear in a hidden assembly return(false); } return(true); }
public Type GetConstructedGenericTypeForHandle(RuntimeTypeHandle typeHandle) { RuntimeTypeHandle genericTypeDefinitionHandle; RuntimeTypeHandle[] genericTypeArgumentHandles; genericTypeDefinitionHandle = RuntimeAugments.GetGenericInstantiation(typeHandle, out genericTypeArgumentHandles); // Reflection blocked constructed generic types simply pretend to not be generic // This is reasonable, as the behavior of reflection blocked types is supposed // to be that they expose the minimal information about a type that is necessary // for users of Object.GetType to move from that type to a type that isn't // reflection blocked. By not revealing that reflection blocked types are generic // we are making it appear as if implementation detail types exposed to user code // are all non-generic, which is theoretically possible, and by doing so // we avoid (in all known circumstances) the very complicated case of representing // the interfaces, base types, and generic parameter types of reflection blocked // generic type definitions. if (ExecutionEnvironment.IsReflectionBlocked(genericTypeDefinitionHandle)) { return(RuntimeBlockedTypeInfo.GetRuntimeBlockedTypeInfo(typeHandle, isGenericTypeDefinition: false)); } RuntimeTypeInfo genericTypeDefinition = genericTypeDefinitionHandle.GetTypeForRuntimeTypeHandle(); int count = genericTypeArgumentHandles.Length; RuntimeTypeInfo[] genericTypeArguments = new RuntimeTypeInfo[count]; for (int i = 0; i < count; i++) { genericTypeArguments[i] = genericTypeArgumentHandles[i].GetTypeForRuntimeTypeHandle(); } return(genericTypeDefinition.GetConstructedGenericType(genericTypeArguments, typeHandle)); }
//======================================================================================= // This group of methods jointly service the Type.GetTypeFromHandle() path. The caller // is responsible for analyzing the RuntimeTypeHandle to figure out which flavor to call. //======================================================================================= public Type GetNamedTypeForHandle(RuntimeTypeHandle typeHandle, bool isGenericTypeDefinition) { QTypeDefinition qTypeDefinition; if (ExecutionEnvironment.TryGetMetadataForNamedType(typeHandle, out qTypeDefinition)) { #if ECMA_METADATA_SUPPORT if (qTypeDefinition.IsNativeFormatMetadataBased) #endif { return(qTypeDefinition.NativeFormatHandle.GetNamedType(qTypeDefinition.NativeFormatReader, typeHandle)); } #if ECMA_METADATA_SUPPORT else { return(System.Reflection.Runtime.TypeInfos.EcmaFormat.EcmaFormatRuntimeNamedTypeInfo.GetRuntimeNamedTypeInfo(qTypeDefinition.EcmaFormatReader, qTypeDefinition.EcmaFormatHandle, typeHandle)); } #endif } else { if (ExecutionEnvironment.IsReflectionBlocked(typeHandle)) { return(RuntimeBlockedTypeInfo.GetRuntimeBlockedTypeInfo(typeHandle, isGenericTypeDefinition)); } else { return(RuntimeNoMetadataNamedTypeInfo.GetRuntimeNoMetadataNamedTypeInfo(typeHandle, isGenericTypeDefinition)); } } }
//======================================================================================= // This group of methods jointly service the Type.GetTypeFromHandle() path. The caller // is responsible for analyzing the RuntimeTypeHandle to figure out which flavor to call. //======================================================================================= public Type GetNamedTypeForHandle(RuntimeTypeHandle typeHandle, bool isGenericTypeDefinition) { MetadataReader reader; TypeDefinitionHandle typeDefinitionHandle; if (ExecutionEnvironment.TryGetMetadataForNamedType(typeHandle, out reader, out typeDefinitionHandle)) { return(typeDefinitionHandle.GetNamedType(reader, typeHandle)); } else { if (ExecutionEnvironment.IsReflectionBlocked(typeHandle)) { return(RuntimeBlockedTypeInfo.GetRuntimeBlockedTypeInfo(typeHandle, isGenericTypeDefinition)); } else { return(RuntimeNoMetadataNamedTypeInfo.GetRuntimeNoMetadataNamedTypeInfo(typeHandle, isGenericTypeDefinition)); } } }