/// <summary>
        ///     Initializes a new instance of the <see cref="Implementation" /> class.
        /// </summary>
        /// <param name="underlyingType">
        ///     The underlying type of the property or collection represented.
        /// </param>
        /// <param name="settings">
        ///     The settings used to control how configuration objects are created and the features they support.
        /// </param>
        internal Implementation(Type underlyingType, ConfigurationObjectSettings settings)
        {
            ImplementationKind = GetImplementationKind(underlyingType, out var isReadOnlyColectionType);
            _settings          = settings;

            Type elementType;

            switch (ImplementationKind)
            {
            case ImplementationKind.ConfigurationObject:
                ImplementationType = ServiceCollectionExtensions.GenerateConfigurationObjectType(underlyingType, settings);
                Type = underlyingType;
                break;

            case ImplementationKind.ConfigurationCollection:
                elementType = underlyingType.GenericTypeArguments.First();
                if (isReadOnlyColectionType)
                {
                    ImplementationType = typeof(ReadOnlyConfigurationCollection <>).MakeGenericType(elementType);
                }
                else
                {
                    ImplementationType = typeof(ConfigurationCollection <>).MakeGenericType(elementType);
                }

                Type = elementType;
                break;

            case ImplementationKind.ConfigurationDictionary:
                elementType = underlyingType.GenericTypeArguments.First();
                if (isReadOnlyColectionType)
                {
                    ImplementationType = typeof(ReadOnlyConfigurationDictionary <>).MakeGenericType(elementType);
                }
                else
                {
                    ImplementationType = typeof(ConfigurationDictionary <>).MakeGenericType(elementType);
                }

                Type = elementType;
                break;

            default:
                ImplementationType = null;
                Type = underlyingType;
                break;
            }
        }
Пример #2
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="ConfigurationObjectBase{TInterface}" /> class. This is the
        ///     interface used when creating the root instance for the service collection.
        /// </summary>
        /// <param name="propertyDef">
        ///     The definition of the property defined by this object. This can be <see lang="null" /> if this object is
        ///     the root of the hierarchy.
        /// </param>
        /// <param name="configurationRoot">
        ///     The configuration root from which to read and write values.
        /// </param>
        /// <param name="parent">
        ///     The parent object to which this one belongs. <see langword="null" /> if this is a root object.
        /// </param>
        /// <param name="settings">
        ///     The settings used to control how configuration objects are created and the features they support.
        /// </param>
        /// <param name="validators">
        ///     The validators that can be applied to configuration objects of this type.
        /// </param>
        protected ConfigurationObjectBase(IPropertyDef?propertyDef, IConfigurationRoot configurationRoot, IConfigurationParent parent, ConfigurationObjectSettings settings, IEnumerable <IConfigurationObjectValidator <TInterface> > validators) : base(propertyDef,
                                                                                                                                                                                                                                                          ServiceCollectionExtensions.GetConfigurationObjectDefinition(typeof(TInterface), settings), configurationRoot, parent)
        {
            _validators = validators?.ToArray() ?? Array.Empty <IConfigurationObjectValidator <TInterface> >();

            DisablePropertyChangedEvents();
            try
            {
                foreach (var value in Values)
                {
                    if (value.PropertyDef.HasDefaultValue)
                    {
                        value.Value = value.PropertyDef.DefaultValue;
                        value.Saved();
                    }
                }
            }
            finally
            {
                EnablePropertyChangedEvents();

                Validate();
            }
        }