/// <summary>
        ///     Initializes a new instance of the <see cref="ConfigurationDictionaryBase{TKey, TElement}" /> class.
        /// </summary>
        /// <param name="parent">
        ///     The parent object to which this one belongs. <see langword="null" /> if this is a root object.
        /// </param>
        /// <param name="propertyDef">
        ///     The definition of the property defined by this object.
        /// </param>
        /// <param name="configurationRoot">
        ///     The configuration root service from which values are read or to which all values will be written.
        /// </param>
        /// <param name="settings">
        ///     The settings used to control how configuration objects are created and the features they support.
        /// </param>
        protected ConfigurationDictionaryBase(IConfigurationParent?parent, IPropertyDef propertyDef, IConfigurationRoot configurationRoot, ConfigurationObjectSettings settings)
        {
            _parent           = parent;
            PropertyDef       = propertyDef;
            ConfigurationRoot = configurationRoot;
            _settings         = settings;

            RegisterReloadToken();
        }
Esempio n. 2
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="ConfigurationDictionary{TElement}" /> class.
 /// </summary>
 /// <param name="parent">
 ///     The parent object to which this one belongs. <see langword="null" /> if this is a root object.
 /// </param>
 /// <param name="propertyDef">
 ///     The definition of the property defined by this object.
 /// </param>
 /// <param name="configurationRoot">
 ///     The configuration root service from which values are read or to which all values will be written.
 /// </param>
 /// <param name="elements">
 ///     A parameter array containing the elements with which to initialize to the collection.
 /// </param>
 /// <param name="settings">
 ///     The settings used to control how configuration objects are created and the features they support.
 /// </param>
 public ReadOnlyConfigurationDictionary(IConfigurationParent?parent, IPropertyDef propertyDef, IConfigurationRoot configurationRoot, ConfigurationObjectSettings settings,
                                        params KeyValuePair <string, TElement>[]?elements) : base(parent, propertyDef, configurationRoot, elements, settings)
 {
 }
        public ConfigurationFixture()
        {
            var settings = new ConfigurationObjectSettings()
            {
                EnableNewtonSoftJsonSupport = true
            };

            var source = new MemoryConfigurationSource()
            {
                InitialData = new[]
                {
                    new KeyValuePair <string, string>(nameof(IRootElement.CharPropertyA), "a"),
                    new KeyValuePair <string, string>(nameof(IRootElement.CharPropertyB), "B"),
                    new KeyValuePair <string, string>(nameof(IRootElement.StringPropertyA), "111"),
                    new KeyValuePair <string, string>(nameof(IRootElement.StringPropertyB), "222"),
                    new KeyValuePair <string, string>(nameof(IRootElement.StringPropertyC), "222"),
                    new KeyValuePair <string, string>(nameof(IRootElement.BooleanPropertyA), "true"),
                    new KeyValuePair <string, string>(nameof(IRootElement.BooleanPropertyB), "false"),
                    new KeyValuePair <string, string>(nameof(IRootElement.Int32PropertyA), "333"),
                    new KeyValuePair <string, string>(nameof(IRootElement.Int32PropertyB), "-444"),
                    new KeyValuePair <string, string>(nameof(IRootElement.Int32PropertyD), "555"),
                    new KeyValuePair <string, string>(nameof(IRootElement.Int16PropertyA), "333"),
                    new KeyValuePair <string, string>(nameof(IRootElement.Int16PropertyB), "-444"),
                    new KeyValuePair <string, string>(nameof(IRootElement.SBytePropertyA), "99"),
                    new KeyValuePair <string, string>(nameof(IRootElement.SBytePropertyB), "-100"),
                    new KeyValuePair <string, string>(nameof(IRootElement.BytePropertyA), "01"),
                    new KeyValuePair <string, string>(nameof(IRootElement.BytePropertyB), "123"),
                    new KeyValuePair <string, string>(nameof(IRootElement.Int64PropertyA), "333"),
                    new KeyValuePair <string, string>(nameof(IRootElement.Int64PropertyB), "-444"),
                    new KeyValuePair <string, string>(nameof(IRootElement.DoublePropertyA), "555.666"),
                    new KeyValuePair <string, string>(nameof(IRootElement.DoublePropertyB), "-666.777"),
                    new KeyValuePair <string, string>(nameof(IRootElement.SinglePropertyA), "555.666"),
                    new KeyValuePair <string, string>(nameof(IRootElement.SinglePropertyB), "-666.777"),
                    new KeyValuePair <string, string>(nameof(IRootElement.SinglePropertyWithDefault), "555.666"),
                    new KeyValuePair <string, string>(nameof(IRootElement.SinglePropertyNoDefault), "-666.777"),
                    new KeyValuePair <string, string>(nameof(IRootElement.DecimalPropertyA), "555.666"),
                    new KeyValuePair <string, string>(nameof(IRootElement.DecimalPropertyB), "-666.777"),
                    new KeyValuePair <string, string>(nameof(IRootElement.DateTimePropertyA), "2020-01-10 18:00:30"),
                    new KeyValuePair <string, string>(nameof(IRootElement.DateTimePropertyB), "2019-10-01 14:30:15"),
                    new KeyValuePair <string, string>(nameof(IRootElement.DateTimeOffsetPropertyA), "2020-01-10 18:00:30 +03:00"),
                    new KeyValuePair <string, string>(nameof(IRootElement.DateTimeOffsetPropertyB), "2019-10-01 14:30:15 +03:00"),
                    new KeyValuePair <string, string>(nameof(IRootElement.TimeSpanPropertyA), "04:00:10"),
                    new KeyValuePair <string, string>(nameof(IRootElement.TimeSpanPropertyB), "00:30:30"),
                    new KeyValuePair <string, string>(nameof(IRootElement.EnumPropertyA), "Public"),
                    new KeyValuePair <string, string>(nameof(IRootElement.EnumPropertyB), "Instance"), new KeyValuePair <string, string>("CustomRoot", "XX_XX"),
                    new KeyValuePair <string, string>("ChildDictionary:Item1:Name", "Item_1"), new KeyValuePair <string, string>("ChildDictionary:Item1:Value", "1"),
                    new KeyValuePair <string, string>("ChildDictionary:Item2:Name", "Item_2"), new KeyValuePair <string, string>("ChildDictionary:Item2:Value", "2"),
                    new KeyValuePair <string, string>("ChildDictionary:Item3:Name", "Item_3"), new KeyValuePair <string, string>("ChildDictionary:Item3:Value", "3"),
                    new KeyValuePair <string, string>("ChildCollection:0:Name", "Item_0"), new KeyValuePair <string, string>("ChildCollection:0:Value", "0"),
                    new KeyValuePair <string, string>("ChildCollection:1:Name", "Item_1"), new KeyValuePair <string, string>("ChildCollection:1:Value", "1"),
                    new KeyValuePair <string, string>("ChildCollection:2:Name", "Item_2"), new KeyValuePair <string, string>("ChildCollection:2:Value", "2"),
                    new KeyValuePair <string, string>(nameof(IRootElement.NonFlagsEnumPropertyA), "First"),
                    new KeyValuePair <string, string>(nameof(IRootElement.NonFlagsEnumPropertyB), "Third"),
                    new KeyValuePair <string, string>(nameof(IRootElement.CustomValueA), "CUSTOM-VALUE-A"),
                    new KeyValuePair <string, string>(nameof(IRootElement.CustomValueC), "VALUE THAT WILL BE IGNORED"),
                    new KeyValuePair <string, string>(nameof(IRootElement.ChildElementProperty) + ":" + nameof(IChildElement.Name), "NAME-1"),
                    new KeyValuePair <string, string>(nameof(IRootElement.ChildElementProperty) + ":" + nameof(IChildElement.Value), "1"),
                    new KeyValuePair <string, string>(nameof(IRootElement.TestDefaults), "false")
                }
            };

            var provider = new MemoryConfigurationProvider(source);

            ConfigurationRoot = new ConfigurationRoot(new[] { provider });

            // Force the Newtonsoft.Json assembly to load.
            var serializer = Newtonsoft.Json.JsonSerializer.Create();

            IServiceCollection servicesCollection = new ServiceCollection();

            servicesCollection.AddSingleton(ConfigurationRoot);
            servicesCollection.AddConfigurationReader <IRootElement>(settings);

            Services = servicesCollection.BuildServiceProvider();

            RootElement = Services.GetService <IRootElement>();
        }
 /// <summary>
 ///     Initializes a new instance of the <see cref="ConfigurationDictionary{TElement}" /> class.
 /// </summary>
 /// <param name="parent">
 ///     The parent object to which this one belongs. <see langword="null" /> if this is a root object.
 /// </param>
 /// <param name="propertyDef">
 ///     The definition of the property defined by this object.
 /// </param>
 /// <param name="configurationRoot">
 ///     The configuration root service from which values are read or to which all values will be written.
 /// </param>
 /// <param name="elements">
 ///     The elements with which to initialize to the collection.
 /// </param>
 /// <param name="settings">
 ///     The settings used to control how configuration objects are created and the features they support.
 /// </param>
 public ConfigurationDictionary(IConfigurationParent?parent, IPropertyDef propertyDef, IConfigurationRoot configurationRoot,
                                IEnumerable <KeyValuePair <string, TElement> >?elements, ConfigurationObjectSettings settings) : base(parent, propertyDef, configurationRoot, elements, settings)
 {
 }
        /// <summary>
        ///     Initializes a new instance of the <see cref="ConfigurationDictionaryBase{TKey, TElement}" /> class.
        /// </summary>
        /// <param name="parent">
        ///     The parent object to which this one belongs. <see langword="null" /> if this is a root object.
        /// </param>
        /// <param name="propertyDef">
        ///     The definition of the property defined by this object.
        /// </param>
        /// <param name="items">
        ///     The elements with which to initialize to the collection.
        /// </param>
        /// <param name="configurationRoot">
        ///     The configuration root service from which values are read or to which all values will be written.
        /// </param>
        /// <param name="settings">
        ///     The settings used to control how configuration objects are created and the features they support.
        /// </param>
        protected ConfigurationDictionaryBase(IConfigurationParent?parent, IPropertyDef propertyDef, IConfigurationRoot configurationRoot,
                                              IEnumerable <KeyValuePair <TKey, TElement> >?items, ConfigurationObjectSettings settings) : this(parent, propertyDef, configurationRoot, settings)
        {
            PropertyDef = propertyDef;

            if (!ReferenceEquals(items, null))
            {
                foreach (var item in items)
                {
                    Element <TKey, TElement> element;
                    _disableReadOnly.Value = _disableReadOnly.Value + 1;
                    try
                    {
                        element = new Element <TKey, TElement>(propertyDef, this, item.Key)
                        {
                            Value = item.Value
                        };
                    }
                    finally
                    {
                        _disableReadOnly.Value = _disableReadOnly.Value - 1;
                    }

                    _itemsByKey.Add(item.Key, element);
                    _orderedItems.Add(element);
                    element.Saved();
                }
            }
        }
 /// <summary>
 ///     Initializes a new instance of the <see cref="ConfigurationCollection{TElement}" /> class.
 /// </summary>
 /// <param name="parent">
 ///     The parent object to which this one belongs. <see langword="null" /> if this is a root object.
 /// </param>
 /// <param name="propertyDef">
 ///     The definition of the property defined by this object.
 /// </param>
 /// <param name="configurationRoot">
 ///     The configuration root service from which values are read or to which all values will be written.
 /// </param>
 /// <param name="elements">
 ///     A parameter array containing the elements with which to initialize to the collection.
 /// </param>
 /// <param name="settings">
 ///     The settings used to control how configuration objects are created and the features they support.
 /// </param>
 public ConfigurationCollection(IConfigurationParent?parent, IPropertyDef propertyDef, IConfigurationRoot configurationRoot, ConfigurationObjectSettings settings,
                                params TElement[]?elements) : base(parent, propertyDef, configurationRoot, GetIndexedElements(elements), settings)
 {
 }
 public ConfigurationObjectMock(IPropertyDef propertyDef, IConfigurationRoot configurationRoot, IConfigurationParent parent, ConfigurationObjectSettings settings) : base(propertyDef, configurationRoot, parent, settings, System.Array.Empty <IConfigurationObjectValidator <IRootElement> >())
 {
 }
 /// <summary>
 ///     Initializes a new instance of the <see cref="ReadOnlyConfigurationCollection{TElement}" /> class.
 /// </summary>
 /// <param name="parent">
 ///     The parent object to which this one belongs. <see langword="null" /> if this is a root object.
 /// </param>
 /// <param name="propertyDef">
 ///     The definition of the property defined by this object.
 /// </param>
 /// <param name="configurationRoot">
 ///     The configuration root service from which values are read or to which all values will be written.
 /// </param>
 /// <param name="elements">
 ///     The elements with which to initialize to the collection.
 /// </param>
 /// <param name="settings">
 ///     The settings used to control how configuration objects are created and the features they support.
 /// </param>
 public ReadOnlyConfigurationCollection(IConfigurationParent?parent, IPropertyDef propertyDef, IConfigurationRoot configurationRoot,
                                        IEnumerable <TElement>?elements, ConfigurationObjectSettings settings) : base(parent, propertyDef, configurationRoot, GetIndexedElements(elements), settings)
 {
 }