private static Type ResolveTypeWithMethodContext(ITypeInfo type, MethodInfo methodContext) { IResolvableTypeInfo resolvableType = type as IResolvableTypeInfo; return(resolvableType != null?resolvableType.Resolve(methodContext, true) : type.Resolve(true)); }
/// <summary> /// Resolves a reflected type to its native <see cref="Type" /> object. /// </summary> /// <param name="type">The reflected type.</param> /// <param name="methodContext">The method that is currently in scope, or null if none. /// This parameter is used when resolving types that are part of the signature /// of a generic method so that generic method arguments can be handled correctly.</param> /// <param name="throwOnError">If true, throws an exception if resolution fails, /// otherwise returns an unresolved <see cref="Type" />.</param> /// <returns>The resolved <see cref="Type" />.</returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="type"/> /// is null.</exception> /// <exception cref="ReflectionResolveException">Thrown if <paramref name="type"/> /// could not be resolved.</exception> public static Type ResolveType(IResolvableTypeInfo type, MethodInfo methodContext, bool throwOnError) { if (type == null) throw new ArgumentNullException("type"); try { ITypeInfo elementType = type.ElementType; if (elementType != null) { Type resolvedElementType = ResolveTypeWithMethodContext(elementType, methodContext); if (type.IsArray) { int rank = type.ArrayRank; if (rank == 1) return resolvedElementType.MakeArrayType(); else return resolvedElementType.MakeArrayType(rank); } else if (type.IsByRef) { return resolvedElementType.MakeByRefType(); } else if (type.IsPointer) { return resolvedElementType.MakePointerType(); } } if (type.IsGenericParameter) { Type resolvedType = ResolveGenericParameter((IGenericParameterInfo)type, methodContext); if (resolvedType != null) return resolvedType; } else { ITypeInfo simpleType = type.GenericTypeDefinition ?? type; Assembly resolvedAssembly = simpleType.Assembly.Resolve(throwOnError); if (! Reflector.IsUnresolved(resolvedAssembly)) { Type resolvedType = resolvedAssembly.GetType(simpleType.FullName); if (resolvedType != null) { if (type.IsGenericType && !type.IsGenericTypeDefinition) { Type[] resolvedTypeArguments = ResolveTypesWithMethodContext(type.GenericArguments, methodContext); resolvedType = resolvedType.MakeGenericType(resolvedTypeArguments); } return resolvedType; } } } } catch (Exception ex) { if (throwOnError) throw new ReflectionResolveException(type, ex); } if (throwOnError) throw new ReflectionResolveException(type); return UnresolvedCodeElementFactory.Instance.Wrap(type); }
/// <summary> /// Resolves a reflected type to its native <see cref="Type" /> object. /// </summary> /// <param name="type">The reflected type.</param> /// <param name="methodContext">The method that is currently in scope, or null if none. /// This parameter is used when resolving types that are part of the signature /// of a generic method so that generic method arguments can be handled correctly.</param> /// <param name="throwOnError">If true, throws an exception if resolution fails, /// otherwise returns an unresolved <see cref="Type" />.</param> /// <returns>The resolved <see cref="Type" />.</returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="type"/> /// is null.</exception> /// <exception cref="ReflectionResolveException">Thrown if <paramref name="type"/> /// could not be resolved.</exception> public static Type ResolveType(IResolvableTypeInfo type, MethodInfo methodContext, bool throwOnError) { if (type == null) { throw new ArgumentNullException("type"); } try { ITypeInfo elementType = type.ElementType; if (elementType != null) { Type resolvedElementType = ResolveTypeWithMethodContext(elementType, methodContext); if (type.IsArray) { int rank = type.ArrayRank; if (rank == 1) { return(resolvedElementType.MakeArrayType()); } else { return(resolvedElementType.MakeArrayType(rank)); } } else if (type.IsByRef) { return(resolvedElementType.MakeByRefType()); } else if (type.IsPointer) { return(resolvedElementType.MakePointerType()); } } if (type.IsGenericParameter) { Type resolvedType = ResolveGenericParameter((IGenericParameterInfo)type, methodContext); if (resolvedType != null) { return(resolvedType); } } else { ITypeInfo simpleType = type.GenericTypeDefinition ?? type; Assembly resolvedAssembly = simpleType.Assembly.Resolve(throwOnError); if (!Reflector.IsUnresolved(resolvedAssembly)) { Type resolvedType = resolvedAssembly.GetType(simpleType.FullName); if (resolvedType != null) { if (type.IsGenericType && !type.IsGenericTypeDefinition) { Type[] resolvedTypeArguments = ResolveTypesWithMethodContext(type.GenericArguments, methodContext); resolvedType = resolvedType.MakeGenericType(resolvedTypeArguments); } return(resolvedType); } } } } catch (Exception ex) { if (throwOnError) { throw new ReflectionResolveException(type, ex); } } if (throwOnError) { throw new ReflectionResolveException(type); } return(UnresolvedCodeElementFactory.Instance.Wrap(type)); }