/// <summary> /// Validates all required dependency properties. /// </summary> /// <remarks> /// This method validates all properties that are marked as required dependency. /// </remarks> /// <param name="source"> /// The source setting. /// </param> /// <param name="others"> /// The list of other settings. /// </param> /// <param name="overall"> /// The list of overall dependency settings. /// </param> /// <exception cref="DependentViolationException"> /// This exception is thrown in case of a dependency violation takes place. /// </exception> private void ValidateRequiredDependencies(ArgumentProcessorSetting source, IEnumerable <ArgumentProcessorSetting> others, IEnumerable <ArgumentProcessorSetting> overall) { if (overall.Count() != others.Count()) { throw new DependentViolationException($"Parameter \"{source.ToParameterLabel()}\" requires {String.Join(" and ", overall.Select(x => $"\"{x.ToParameterLabel()}\""))}."); } }
/// <summary> /// Tries to apply a default value to its related property. /// </summary> /// <remarks> /// This method tries to apply a default value to its related property. For /// the moment, only optional parameters support a usage of default values. /// </remarks> /// <param name="setting"> /// An instance of class <see cref="ArgumentProcessorSetting"/> to get the /// default value from to be applied. /// </param> /// <returns> /// The same instance of class <see cref="ArgumentProcessorSetting"/> that /// has been provided as parameter. /// </returns> /// <exception cref="DefaultValueException"> /// This exception is thrown in all cases of applying a default value fails. /// </exception> private ArgumentProcessorSetting ApplyDefaultValue(ArgumentProcessorSetting setting) { if (!(setting.Attribute is OptionParameterAttribute attribute)) { return(setting); } if (!attribute.HasDefaultValue) { return(setting); } try { setting.Property.SetValue(this.Instance, OptionTypeConverter.Convert(attribute.DefaultValue?.ToString(), setting.Property.PropertyType)); } catch (Exception exception) { throw new DefaultValueException( $"Could not apply default value of \"{(attribute.DefaultValue is null ? "null" : attribute.DefaultValue.ToString())}\" " + $"to property \"{setting.Property.Name}\". See inner exception for more information.", exception); } return(setting); }
/// <summary> /// Validates all optional dependency properties. /// </summary> /// <remarks> /// This method validates all properties that are marked as optional dependency. /// </remarks> /// <param name="source"> /// The source setting. /// </param> /// <param name="others"> /// The list of other settings. /// </param> /// <param name="overall"> /// The list of overall dependency settings. /// </param> /// <exception cref="DependentViolationException"> /// This exception is thrown in case of a dependency violation takes place. /// </exception> private void ValidateOptionalDependencies(ArgumentProcessorSetting source, IEnumerable <ArgumentProcessorSetting> others, IEnumerable <ArgumentProcessorSetting> overall) { if (overall.Any() && !others.Any()) { throw new DependentViolationException($"Parameter \"{source.ToParameterLabel()}\" depends on {String.Join(" or ", overall.Select(x => $"\"{x.ToParameterLabel()}\""))}."); } }
/// <summary> /// Tries to get referenced items. /// </summary> /// <remarks> /// This method tries to extract all referenced items from current settings list. /// </remarks> /// <param name="source"> /// The source setting. /// </param> /// <param name="others"> /// The list of source settings. /// </param> /// <param name="overall"> /// The list of referenced overall settings. /// </param> /// <returns> /// The result list of referenced settings. /// </returns> private IEnumerable <ArgumentProcessorSetting> TryGetReferencedItems(ArgumentProcessorSetting source, IEnumerable <ArgumentProcessorSetting> others, out IEnumerable <ArgumentProcessorSetting> overall) { // Get list of dependencies... String[] affected = source.Attribute.GetDependencies(); // Apply all affected dependencies from overall settings. overall = this.Settings.Where(x => affected.Contains(x.Property.Name)); // Filter out all currently available dependencies and return them. return(others.Where(x => affected.Contains(x.Property.Name))); }
/// <summary> /// Validates all referenced properties. /// </summary> /// <remarks> /// This method validates all properties that are referenced by other properties. /// </remarks> /// <param name="source"> /// The source setting. /// </param> /// <exception cref="DependentViolationException"> /// This exception is thrown in case of a dependency violation takes place. /// </exception> private void ValidateReferencedProperties(ArgumentProcessorSetting source) { IEnumerable <String> candidates = source.Attribute.GetDependencies(); IEnumerable <String> properties = this.Settings.Where(x => candidates.Contains(x.Property.Name)).Select(x => x.Property.Name); if (candidates.Count() != properties.Count()) { String[] missings = candidates.Except(properties).ToArray(); String s = missings.Length > 1 ? "s" : String.Empty; throw new DependentViolationException($"Unable to confirm dependency name{s} {String.Join(" and ", missings.Select(x => $"\"{x}\""))} as property name{s}."); } }
/// <summary> /// Try finding a particular setting. /// </summary> /// <remarks> /// This method tries to retrieve a particular setting value from current settings list. /// </remarks> /// <param name="setting"> /// The found setting or <c>null</c>. /// </param> /// <returns> /// True, if the setting could be found and false if not. /// </returns> private Boolean TryFindSetting(out ArgumentProcessorSetting setting) { setting = null; foreach (ArgumentProcessorSetting current in this.Settings) { if (current.IsVerbalParameter()) { setting = current; return(true); } } return(false); }
/// <summary> /// Try finding a particular setting by parameter name. /// </summary> /// <remarks> /// This method tries to retrieve a particular setting value from current settings list. /// </remarks> /// <param name="parameter"> /// The parameter name to search for. /// </param> /// <param name="setting"> /// The found setting or <c>null</c>. /// </param> /// <returns> /// True, if the setting could be found and false if not. /// </returns> private Boolean TryFindSetting(String parameter, out ArgumentProcessorSetting setting) { setting = null; parameter = parameter.RemovePrefix(); foreach (ArgumentProcessorSetting current in this.Settings) { if (current.Attribute.IsSolidLabelAndStartsWith(parameter) || current.Attribute.IsBriefLabelAndStartsWith(parameter)) { setting = current; return(true); } } return(false); }