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)); }
public static RuntimeTypeInfo GetConstructedGenericType(this RuntimeTypeInfo genericTypeDefinition, RuntimeTypeInfo[] genericTypeArguments) { return(genericTypeDefinition.GetConstructedGenericType(genericTypeArguments, default(RuntimeTypeHandle))); }
// // Main routine to parse a metadata type specification signature. // private static RuntimeTypeInfo TryResolveTypeSignature(this TypeSpecificationHandle typeSpecHandle, MetadataReader reader, 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. } RuntimeTypeInfo elementType = sig.ElementType.TryResolve(reader, typeContext, ref exception); if (elementType == null) { return(null); } return(elementType.GetMultiDimArrayType(rank)); } case HandleType.ByReferenceSignature: { ByReferenceSignature sig = typeHandle.ToByReferenceSignatureHandle(reader).GetByReferenceSignature(reader); RuntimeTypeInfo targetType = sig.Type.TryResolve(reader, typeContext, ref exception); if (targetType == null) { return(null); } return(targetType.GetByRefType()); } 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); RuntimeTypeInfo targetType = sig.Type.TryResolve(reader, typeContext, ref exception); if (targetType == null) { return(null); } return(targetType.GetPointerType()); } case HandleType.SZArraySignature: { SZArraySignature sig = typeHandle.ToSZArraySignatureHandle(reader).GetSZArraySignature(reader); RuntimeTypeInfo elementType = sig.ElementType.TryResolve(reader, typeContext, ref exception); if (elementType == null) { return(null); } return(elementType.GetArrayType()); } case HandleType.TypeDefinition: { return(typeHandle.ToTypeDefinitionHandle(reader).ResolveTypeDefinition(reader)); } case HandleType.TypeInstantiationSignature: { TypeInstantiationSignature sig = typeHandle.ToTypeInstantiationSignatureHandle(reader).GetTypeInstantiationSignature(reader); RuntimeTypeInfo genericTypeDefinition = sig.GenericType.TryResolve(reader, typeContext, ref exception); if (genericTypeDefinition == null) { return(null); } LowLevelList <RuntimeTypeInfo> genericTypeArguments = new LowLevelList <RuntimeTypeInfo>(); foreach (Handle genericTypeArgumentHandle in sig.GenericTypeArguments) { RuntimeTypeInfo genericTypeArgument = genericTypeArgumentHandle.TryResolve(reader, typeContext, ref exception); if (genericTypeArgument == null) { return(null); } genericTypeArguments.Add(genericTypeArgument); } return(genericTypeDefinition.GetConstructedGenericType(genericTypeArguments.ToArray())); } case HandleType.TypeReference: { return(typeHandle.ToTypeReferenceHandle(reader).TryResolveTypeReference(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. } }