/// <summary> /// Gets the Value Registry representing the setting provided. /// </summary> /// <param name="settingName">The name of the setting already registered (case sensitive).</param> /// <returns>The IValueRegistry object representing the setting provided.</returns> /// <exception cref="ValueRegistryNotFoundException">If the provided setting name is not found to be registered.</exception> public IValueRegistry GetSettingRegistry(string settingName) { if (string.IsNullOrWhiteSpace(settingName)) { throw new ArgumentNullException(nameof(settingName), $"Cannot get the {nameof(ValueRegistry)} of a NULL or whitespace setting name."); } IValueRegistry registry = _values.FirstOrDefault(val => val?.Name == settingName) as ValueRegistry; if (registry == default(ValueRegistry)) { throw new ValueRegistryNotFoundException($"{nameof(ValueRegistry)} for Setting Name \"{settingName}\" could not be found."); } return(registry); }
private static void ValidateSettingHasValueOrIsNotRequired(string settingName, IValueRegistry registry) { if (!ApplicationSettings.HasValue(settingName) && (registry?.Required ?? ValueRegistry.DEFAULT_REQUIRED)) { throw new ConfigurationErrorsException($"No value for setting \"{settingName}\" is provided in the Configuration and this setting is marked as required."); } }
private static bool TryGetValueAsType(string settingName, IValueRegistry registry, out dynamic value) { value = null; if (registry?.ValueType == null) { return(false); } try { // NOTE: C# 7.0+ feature of pattern matching (Visual Studio 2017+ required) switch (registry.ValueType) { case Type _ when registry.ValueType == typeof(string): value = ApplicationSettings.AsString(settingName); break; case Type _ when registry.ValueType == typeof(char): value = ApplicationSettings.AsChar(settingName); break; case Type _ when registry.ValueType == typeof(bool): value = ApplicationSettings.AsBool(settingName); break; case Type _ when registry.ValueType == typeof(byte): value = ApplicationSettings.AsByte(settingName); break; case Type _ when registry.ValueType == typeof(sbyte): value = ApplicationSettings.AsSByte(settingName); break; case Type _ when registry.ValueType == typeof(decimal): value = ApplicationSettings.AsDecimal(settingName); break; case Type _ when registry.ValueType == typeof(double): value = ApplicationSettings.AsDouble(settingName); break; case Type _ when registry.ValueType == typeof(float): value = ApplicationSettings.AsFloat(settingName); break; case Type _ when registry.ValueType == typeof(int): value = ApplicationSettings.AsInt(settingName); break; case Type _ when registry.ValueType == typeof(uint): value = ApplicationSettings.AsUInt(settingName); break; case Type _ when registry.ValueType == typeof(long): value = ApplicationSettings.AsLong(settingName); break; case Type _ when registry.ValueType == typeof(ulong): value = ApplicationSettings.AsULong(settingName); break; case Type _ when registry.ValueType == typeof(short): value = ApplicationSettings.AsShort(settingName); break; case Type _ when registry.ValueType == typeof(ushort): value = ApplicationSettings.AsUShort(settingName); break; default: if (registry.CustomTypeCastFunction == null) { throw new InvalidCastException($"Failed to retrieve \"{settingName}\" as Type \"{registry.ValueType.FullName}\"."); } value = registry.CustomTypeCastFunction(ApplicationSettings.AsString(settingName)); break; } return(true); } catch (InvalidCastException) { throw; } catch (Exception) { return(false); } }