/// <summary> /// Initializes a new instance of the <see cref="ConfigReloadingProxy{TInterface}"/> class. /// </summary> /// <param name="section">The configuration section that defines the object that this class creates.</param> /// <param name="defaultTypes"> /// An object that defines the default types to be used when a type is not explicitly specified by a /// configuration section. /// </param> /// <param name="valueConverters"> /// An object that defines custom converter functions that are used to convert string configuration /// values to a target type. /// </param> /// <param name="declaringType">If present the declaring type of the member that this instance is a value of.</param> /// <param name="memberName">If present, the name of the member that this instance is the value of.</param> /// <param name="resolver"> /// An object that can retrieve constructor parameter values that are not found in configuration. This /// object is an adapter for dependency injection containers, such as Ninject, Unity, Autofac, or /// StructureMap. Consider using the <see cref="Resolver"/> class for this parameter, as it supports /// most depenedency injection containers. /// </param> protected ConfigReloadingProxy(IConfiguration section, DefaultTypes defaultTypes, ValueConverters valueConverters, Type declaringType, string memberName, IResolver resolver) { if (typeof(TInterface) == typeof(IEnumerable)) { throw new InvalidOperationException("The IEnumerable interface is not supported."); } if (typeof(IEnumerable).GetTypeInfo().IsAssignableFrom(typeof(TInterface))) { throw new InvalidOperationException($"Interfaces that inherit from IEnumerable are not suported: '{typeof(TInterface).FullName}'"); } _section = section ?? throw new ArgumentNullException(nameof(section)); _defaultTypes = defaultTypes ?? ConfigurationObjectFactory.EmptyDefaultTypes; _valueConverters = valueConverters ?? ConfigurationObjectFactory.EmptyValueConverters; _declaringType = declaringType; // Null is a valid value _memberName = memberName; // Null is a valid value _resolver = resolver ?? Resolver.Empty; _hash = GetHash(); Object = CreateObject(); ChangeToken.OnChange(section.GetReloadToken, () => ReloadObject(false)); }
/// <summary> /// Create an object of type <paramref name="interfaceType"/> based on the specified configuration. The returned /// object delegates its functionality to a backing field that is reloaded when the configuration changes. /// </summary> /// <param name="configuration">The configuration to create the object from.</param> /// <param name="interfaceType">The interface type to create.</param> /// <param name="defaultTypes"> /// An object that defines the default types to be used when a type is not explicitly specified by a /// configuration section. /// </param> /// <param name="valueConverters"> /// An object that defines custom converter functions that are used to convert string configuration /// values to a target type. /// </param> /// <returns> /// An object of type <paramref name="interfaceType"/> with values set from the configuration that reloads /// itself when the configuration changes. /// </returns> public static object CreateReloadingProxy(this IConfiguration configuration, Type interfaceType, DefaultTypes defaultTypes, ValueConverters valueConverters) => configuration.CreateReloadingProxy(interfaceType, defaultTypes, valueConverters, null);
/// <summary> /// Create an object of type <typeparamref name="TInterface"/> based on the specified configuration. The returned /// object delegates its functionality to a backing field that is reloaded when the configuration changes. /// </summary> /// <typeparam name="TInterface">The interface type to create.</typeparam> /// <param name="configuration">The configuration to create the object from.</param> /// <param name="defaultTypes"> /// An object that defines the default types to be used when a type is not explicitly specified by a /// configuration section. /// </param> /// <param name="valueConverters"> /// An object that defines custom converter functions that are used to convert string configuration /// values to a target type. /// </param> /// <param name="resolver"> /// An object that can retrieve constructor parameter values that are not found in configuration. This /// object is an adapter for dependency injection containers, such as Ninject, Unity, Autofac, or /// StructureMap. Consider using the <see cref="Resolver"/> class for this parameter, as it supports /// most depenedency injection containers. /// </param> /// <returns> /// An object of type <typeparamref name="TInterface"/> with values set from the configuration that reloads /// itself when the configuration changes. /// </returns> public static TInterface CreateReloadingProxy <TInterface>(this IConfiguration configuration, DefaultTypes defaultTypes = null, ValueConverters valueConverters = null, IResolver resolver = null) => (TInterface)configuration.CreateReloadingProxy(typeof(TInterface), defaultTypes, valueConverters, resolver);
/// <summary> /// Create an object of type <typeparamref name="TInterface"/> based on the specified configuration. The returned /// object delegates its functionality to a backing field that is reloaded when the configuration changes. /// </summary> /// <typeparam name="TInterface">The interface type to create.</typeparam> /// <param name="configuration">The configuration to create the object from.</param> /// <param name="defaultTypes"> /// An object that defines the default types to be used when a type is not explicitly specified by a /// configuration section. /// </param> /// <param name="valueConverters"> /// An object that defines custom converter functions that are used to convert string configuration /// values to a target type. /// </param> /// <returns> /// An object of type <typeparamref name="TInterface"/> with values set from the configuration that reloads /// itself when the configuration changes. /// </returns> public static TInterface CreateReloadingProxy <TInterface>(this IConfiguration configuration, DefaultTypes defaultTypes, ValueConverters valueConverters) => configuration.CreateReloadingProxy <TInterface>(defaultTypes, valueConverters, null);
internal static object CreateReloadingProxy(this IConfiguration configuration, Type interfaceType, DefaultTypes defaultTypes, ValueConverters valueConverters, Type declaringType, string memberName, IResolver resolver) { if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if (interfaceType == null) { throw new ArgumentNullException(nameof(interfaceType)); } if (!interfaceType.GetTypeInfo().IsInterface) { throw new ArgumentException($"Specified type is not an interface: '{interfaceType.FullName}'.", nameof(interfaceType)); } if (interfaceType == typeof(IEnumerable)) { throw new ArgumentException("The IEnumerable interface is not supported."); } if (typeof(IEnumerable).GetTypeInfo().IsAssignableFrom(interfaceType)) { throw new ArgumentException($"Interfaces that inherit from IEnumerable are not suported: '{interfaceType.FullName}'", nameof(interfaceType)); } if (!string.IsNullOrEmpty(configuration[ConfigurationObjectFactory.TypeKey]) && string.Equals(configuration[ConfigurationObjectFactory.ReloadOnChangeKey], "false", StringComparison.OrdinalIgnoreCase)) { return(configuration.BuildTypeSpecifiedObject(interfaceType, declaringType, memberName, valueConverters ?? new ValueConverters(), defaultTypes ?? new DefaultTypes(), resolver)); } var createReloadingProxy = _proxyFactories.GetOrAdd(interfaceType, CreateProxyTypeFactoryMethod); return(createReloadingProxy.Invoke(configuration, defaultTypes, valueConverters, declaringType, memberName, resolver)); }
/// <summary> /// Create an object of type <paramref name="interfaceType"/> based on the specified configuration. The returned /// object delegates its functionality to a backing field that is reloaded when the configuration changes. /// </summary> /// <param name="configuration">The configuration to create the object from.</param> /// <param name="interfaceType">The interface type to create.</param> /// <param name="defaultTypes"> /// An object that defines the default types to be used when a type is not explicitly specified by a /// configuration section. /// </param> /// <param name="valueConverters"> /// An object that defines custom converter functions that are used to convert string configuration /// values to a target type. /// </param> /// <param name="resolver"> /// An object that can retrieve constructor parameter values that are not found in configuration. This /// object is an adapter for dependency injection containers, such as Ninject, Unity, Autofac, or /// StructureMap. Consider using the <see cref="Resolver"/> class for this parameter, as it supports /// most depenedency injection containers. /// </param> /// <returns> /// An object of type <paramref name="interfaceType"/> with values set from the configuration that reloads /// itself when the configuration changes. /// </returns> public static object CreateReloadingProxy(this IConfiguration configuration, Type interfaceType, DefaultTypes defaultTypes = null, ValueConverters valueConverters = null, IResolver resolver = null) => configuration.CreateReloadingProxy(interfaceType, defaultTypes, valueConverters, null, null, resolver ?? Resolver.Empty);