示例#1
0
        public void SetItemValue_ExistingEmptyItem(Type type, object value)
        {
            // create an item without a value
            // --------------------------------------------------------------------------------------------------------
            MethodInfo method = mConfiguration.GetType()
                                .GetMethods()
                                .Where(x => x.Name == nameof(CascadedConfiguration.SetItem) && x.GetParameters().Count() == 1)
                                .Where(x => x.IsGenericMethodDefinition && x.GetGenericArguments().Count() == 1)
                                .Where(x => x.GetParameters().Count() == 1)
                                .Single()
                                .MakeGenericMethod(type);

            ICascadedConfigurationItem item = (ICascadedConfigurationItem)method.Invoke(mConfiguration, new[] { "Value" });

            Assert.False(item.HasValue);
            Assert.Equal(type, item.Type);
            Assert.Throws <ConfigurationException>(() => item.Value);

            // set the value
            // --------------------------------------------------------------------------------------------------------
            item.Value = value;
            Assert.True(item.HasValue);
            Assert.Equal(type, item.Type);
            Assert.Equal(value, item.Value);
        }
示例#2
0
        public void SetValue(Type type, object value)
        {
            MethodInfo method = mConfiguration.GetType()
                                .GetMethods()
                                .Where(x => x.Name == nameof(CascadedConfiguration.SetValue) && x.GetParameters().Count() == 2)
                                .Where(x => x.IsGenericMethodDefinition && x.GetGenericArguments().Count() == 1)
                                .Single()
                                .MakeGenericMethod(type);

            ICascadedConfigurationItem item = (ICascadedConfigurationItem)method.Invoke(mConfiguration, new[] { "Value", value });

            Assert.True(item.HasValue);
            Assert.Equal(type, item.Type);
            Assert.Equal(value, item.Value);
        }
        /// <summary>
        /// Loads the value of the specified configuration item from the persistent storage.
        /// </summary>
        /// <param name="item">Item to load.</param>
        public override void LoadItem(ICascadedConfigurationItem item)
        {
            CascadedConfiguration configuration = item.Configuration.RootConfiguration;

            lock (configuration.Sync)
            {
                if (mXmlDocument != null)
                {
                    XmlElement root = mXmlDocument.SelectSingleNode("//ConfigurationFile") as XmlElement;
                    if (root != null)
                    {
                        XmlElement rootConfigurationElement = root.SelectSingleNode(string.Format("Configuration[@name='{0}']", configuration.Name)) as XmlElement;
                        if (rootConfigurationElement != null)
                        {
                            LoadItemInternal(item, item.Path.TrimStart('/'), rootConfigurationElement);
                        }
                    }
                }
            }
        }
示例#4
0
        /// <summary>
        /// Tries to save the specified configuration item using a converter.
        /// </summary>
        /// <param name="item">Configuration item to save.</param>
        /// <param name="keyPath">Path of the registry key to save the configuration to.</param>
        /// <returns>true, if saving the configuration item succeeded; otherwise false.</returns>
        private bool SaveUsingConverter(ICascadedConfigurationItem item, string keyPath)
        {
            // try to get a converter that has been registered with the configuration
            IConverter converter = GetValueConverter(item.Type);

            if (converter == null)
            {
                // the configuration does not have a registered converter for the type of the configuration item
                // => try to get a converter
                converter = Converters.GetGlobalConverter(item.Type);
                if (converter == null)
                {
                    return(false);
                }
            }

            // convert the object to a string and save it in the registry
            string value = converter.ConvertObjectToString(item.Value, CultureInfo.InvariantCulture);

            Registry.SetValue(keyPath, item.Name, value, RegistryValueKind.String);
            return(true);
        }
        /// <summary>
        /// Recursion helper for the <see cref="LoadItem(ICascadedConfigurationItem)"/> method.
        /// </summary>
        /// <param name="item">Item to load.</param>
        /// <param name="remainingPath">Remaining path to the requested item.</param>
        /// <param name="parent">XML node of the parent configuration.</param>
        private void LoadItemInternal(ICascadedConfigurationItem item, string remainingPath, XmlElement parent)
        {
            string[] pathTokens = CascadedConfigurationPathHelper.SplitPath(this, remainingPath, true, true);

            if (pathTokens.Length > 1)
            {
                string     itemName             = CascadedConfigurationPathHelper.UnescapeName(pathTokens[0]);
                XmlElement configurationElement = parent.SelectSingleNode(string.Format("Configuration[@name='{0}']", itemName)) as XmlElement;
                if (configurationElement != null)
                {
                    LoadItemInternal(item, string.Join("/", pathTokens, 1, pathTokens.Length - 1), configurationElement);
                }
            }
            else
            {
                XmlElement itemElement = parent.SelectSingleNode(string.Format("Item[@name='{0}']", item.Name)) as XmlElement;
                if (itemElement != null)
                {
                    object value = GetValueFromXmlElement(itemElement, item.Name, item.Type);
                    item.Value = value;
                }
            }
        }
示例#6
0
 /// <summary>
 /// Loads the value of the specified configuration item from the persistent storage.
 /// </summary>
 /// <param name="item">Item to load.</param>
 public override void LoadItem(ICascadedConfigurationItem item)
 {
     // not supported, yet.
     // (all configurations items must be added before the settings are loaded)
 }
 /// <summary>
 /// Loads the value of the specified configuration item from the persistent storage.
 /// </summary>
 /// <param name="item">Item to load.</param>
 public abstract void LoadItem(ICascadedConfigurationItem item);