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); }
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); } } } } }
/// <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; } } }
/// <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);