/// <summary> /// Tries to load type which is either specified by <paramref name="assemblyLocation"/> and <paramref name="typeName"/> pair, or by assembly-qualified <paramref name="typeName"/>. /// </summary> /// <param name="loader">This <see cref="ExplicitAssemblyLoader"/>.</param> /// <param name="typeName">The name of the type. It should be the full name if <paramref name="assemblyLocation"/> is specified, and assembly-qualified name if <paramref name="assemblyLocation"/> is not specified. It can be <c>null</c> if <paramref name="assemblyLocation"/> and <paramref name="requiredParentType"/> are both specified; in that case the first suitable type is returned.</param> /// <param name="assemblyLocation">The location of the assembly file. If it is specified, this <see cref="ExplicitAssemblyLoader"/> is not used. Instead, the type is loaded using <see cref="Type.GetType(string, bool)"/>, if <paramref name="typeName"/> is specified.</param> /// <param name="requiredParentType">Optional parent type which the loaded type must be assignable from.</param> /// <param name="additionalTypeCheck">Optional additional callback to check loaded type.</param> /// <returns>The loaded type, or <c>null</c>.</returns> /// <exception cref="NullReferenceException">If <paramref name="assemblyLocation"/> is specified, and this <see cref="ExplicitAssemblyLoader"/> is <c>null</c>.</exception> public static Type TryLoadTypeFromAssembly( this ExplicitAssemblyLoader loader, String typeName, String assemblyLocation, TypeInfo requiredParentType = null, Func <Type, Boolean> additionalTypeCheck = null ) { var providerTypeNameSpecified = !String.IsNullOrEmpty(typeName); Type providerType; //String errorMessage; if (String.IsNullOrEmpty(assemblyLocation)) { if (providerTypeNameSpecified) { providerType = Type.GetType(typeName, false); //errorMessage = $"Failed to load type \"{providerTypeName}\", make sure the name is assembly-qualified."; } else { providerType = null; //errorMessage = $"The task must receive {nameof( ConnectionPoolProviderAssemblyLocation )} and/or {nameof( ConnectionPoolProviderTypeName )} parameters."; } } else { var providerAssembly = ArgumentValidator.ValidateNotNullReference(loader).LoadAssemblyFrom(assemblyLocation, true).LoadedAssembly; if (providerTypeNameSpecified) { providerType = providerAssembly.GetType(typeName, false); //errorMessage = $"No type \"{providerTypeName}\" in assembly located in \"{providerAssemblyLocation}\"."; } else if (requiredParentType != null) { providerType = providerAssembly.DefinedTypes.FirstOrDefault(t => requiredParentType.IsAssignableFrom(t))?.AsType(); //errorMessage = $"Failed to find any type implementing \"{providerBaseType.AssemblyQualifiedName}\" in assembly located in \"{providerAssemblyLocation}\"."; } else { providerType = null; } } return(providerType != null && (requiredParentType?.IsAssignableFrom(providerType) ?? true) && (additionalTypeCheck?.Invoke(providerType) ?? true) ? providerType : null); }
/// <summary> /// Tries to create an instance of struct which is either specified by <paramref name="assemblyLocation"/> and <paramref name="typeName"/> pair, or by assembly-qualified <paramref name="typeName"/>. /// </summary> /// <typeparam name="T">The type of the struct.</typeparam> /// <param name="loader">This <see cref="ExplicitAssemblyLoader"/>.</param> /// <param name="typeName">The name of the type. It should be the full name if <paramref name="assemblyLocation"/> is specified, and assembly-qualified name if <paramref name="assemblyLocation"/> is not specified. It can be <c>null</c> if <paramref name="assemblyLocation"/> is specified; in that case the first suitable type is returned.</param> /// <param name="assemblyLocation">The location of the assembly file. If it is specified, this <see cref="ExplicitAssemblyLoader"/> is not used. Instead, the type is loaded using <see cref="Type.GetType(string, bool)"/>, if <paramref name="typeName"/> is specified.</param> /// <param name="instanceCreator">The optional callback to create an instance once the type has been acquired. By default, the <see cref="Activator.CreateInstance(Type)"/> is used.</param> /// <returns>An instance of type <typeparamref name="T"/>, or <c>null</c>.</returns> /// <exception cref="NullReferenceException">If <paramref name="assemblyLocation"/> is specified, and this <see cref="ExplicitAssemblyLoader"/> is <c>null</c>.</exception> public static T?TryLoadStructInstanceFromAssembly <T>( this ExplicitAssemblyLoader loader, String typeName, String assemblyLocation, Func <Type, Object> instanceCreator = null ) where T : struct { return((T?)(loader.TryLoadInstanceFromAssembly( typeName, assemblyLocation, additionalTypeCheck: t => !t.GetTypeInfo().IsClass, instanceCreator: instanceCreator ))); }
/// <summary> /// Tries to create an instance of class which is either specified by <paramref name="assemblyLocation"/> and <paramref name="typeName"/> pair, or by assembly-qualified <paramref name="typeName"/>. /// </summary> /// <typeparam name="T">The type of the class.</typeparam> /// <param name="loader">This <see cref="ExplicitAssemblyLoader"/>.</param> /// <param name="typeName">The name of the type. It should be the full name if <paramref name="assemblyLocation"/> is specified, and assembly-qualified name if <paramref name="assemblyLocation"/> is not specified. It can be <c>null</c> if <paramref name="assemblyLocation"/> is specified; in that case the first suitable type is returned.</param> /// <param name="assemblyLocation">The location of the assembly file. If it is specified, this <see cref="ExplicitAssemblyLoader"/> is not used. Instead, the type is loaded using <see cref="Type.GetType(string, bool)"/>, if <paramref name="typeName"/> is specified.</param> /// <param name="instanceCreator">The optional callback to create an instance once the type has been acquired. By default, the <see cref="Activator.CreateInstance(Type)"/> is used.</param> /// <returns>An instance of type <typeparamref name="T"/>, or <c>null</c>.</returns> /// <exception cref="NullReferenceException">If <paramref name="assemblyLocation"/> is specified, and this <see cref="ExplicitAssemblyLoader"/> is <c>null</c>.</exception> public static T TryLoadClassInstanceFromAssembly <T>( this ExplicitAssemblyLoader loader, String typeName, String assemblyLocation, Func <Type, Object> instanceCreator = null ) where T : class { return((T)(loader.TryLoadInstanceFromAssembly( typeName, assemblyLocation, requiredParentType: typeof(T).GetTypeInfo(), additionalTypeCheck: t => t.GetTypeInfo().IsClass, instanceCreator: instanceCreator ))); }
/// <summary> /// Tries to create an instance of type which is either specified by <paramref name="assemblyLocation"/> and <paramref name="typeName"/> pair, or by assembly-qualified <paramref name="typeName"/>. /// </summary> /// <param name="loader">This <see cref="ExplicitAssemblyLoader"/>.</param> /// <param name="typeName">The name of the type. It should be the full name if <paramref name="assemblyLocation"/> is specified, and assembly-qualified name if <paramref name="assemblyLocation"/> is not specified. It can be <c>null</c> if <paramref name="assemblyLocation"/> and <paramref name="requiredParentType"/> are both specified; in that case the first suitable type is returned.</param> /// <param name="assemblyLocation">The location of the assembly file. If it is specified, this <see cref="ExplicitAssemblyLoader"/> is not used. Instead, the type is loaded using <see cref="Type.GetType(string, bool)"/>, if <paramref name="typeName"/> is specified.</param> /// <param name="requiredParentType">Optional parent type which the loaded type must be assignable from.</param> /// <param name="additionalTypeCheck">Optional additional callback to check loaded type.</param> /// <param name="instanceCreator">The optional callback to create an instance once the type has been acquired. By default, the <see cref="Activator.CreateInstance(Type)"/> is used.</param> /// <returns>An instance of given type, or <c>null</c>.</returns> /// <exception cref="NullReferenceException">If <paramref name="assemblyLocation"/> is specified, and this <see cref="ExplicitAssemblyLoader"/> is <c>null</c>.</exception> public static Object TryLoadInstanceFromAssembly( this ExplicitAssemblyLoader loader, String typeName, String assemblyLocation, TypeInfo requiredParentType = null, Func <Type, Boolean> additionalTypeCheck = null, Func <Type, Object> instanceCreator = null ) { var type = loader.TryLoadTypeFromAssembly( typeName, assemblyLocation, requiredParentType: requiredParentType, additionalTypeCheck: additionalTypeCheck ); return(type == null ? null : (instanceCreator?.Invoke(type) ?? Activator.CreateInstance(type))); }