예제 #1
0
        /// <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 dependency 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 supported: '{typeof(TInterface).FullName}'");
            }

            _section         = section ?? throw new ArgumentNullException(nameof(section));
            _defaultTypes    = defaultTypes ?? ConfigurationObjectFactory.EmptyDefaultTypes;
            _valueConverters = valueConverters ?? ConfigurationObjectFactory.EmptyValueConverters;
            _declaringType   = declaringType;
            _memberName      = memberName;
            _resolver        = resolver ?? Resolver.Empty;
            _hash            = GetHash();

            Object = CreateObject();

            ChangeToken.OnChange(section.GetReloadToken, () => ReloadObject(false));
        }
        internal static object CreateReloadingProxy(this IConfiguration configuration, Type interfaceType,
                                                    DefaultTypes defaultTypes, ValueConverters valueConverters, Type declaringType, string memberName,
                                                    IResolver resolver)
        {
            if (configuration.IsNull())
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            if (interfaceType.IsNull())
            {
                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 supported: '{interfaceType.FullName}'",
                          nameof(interfaceType));
            }

            if (configuration[ConfigurationObjectFactory.TypeKey].IsNotNullOrEmpty() &&
                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 <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)
 {
     return(configuration.CreateReloadingProxy <TInterface>(defaultTypes, valueConverters, null));
 }
 /// <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 dependency 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)
 {
     return(configuration.CreateReloadingProxy(interfaceType, defaultTypes, valueConverters, null, null,
                                               resolver ?? Resolver.Empty));
 }
 /// <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)
 {
     return(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 dependency 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)
 {
     return((TInterface)configuration.CreateReloadingProxy(typeof(TInterface), defaultTypes, valueConverters,
                                                           resolver));
 }