using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; namespace DynamicDescriptors; /// /// A that provides a collection of standard values. /// public sealed class StandardValuesStringConverter : StringConverter { /// /// An empty StandardValuesCollection, to be returned when the values factory is not populated. /// private static readonly StandardValuesCollection EmptyStandardValuesCollection = new StandardValuesCollection(new string[] { }); /// /// A factory that supplies the standard values to be supported. /// private readonly Func _valuesFactory; /// /// Initializes a new instance of the class. /// /// /// An enumerator that iterates through the sequence of standard values to be supported. /// public StandardValuesStringConverter(IEnumerable values) { _valuesFactory = values != null ? () => values.ToArray() : null; } /// /// Initializes a new instance of the class. /// /// A function that supplies the standard values to be supported. public StandardValuesStringConverter(Func valuesFactory) { _valuesFactory = valuesFactory; } /// /// Returns a value indicating whether this object supports a standard set of values that /// can be picked from a list, using the specified context. /// /// /// An that provides a format context. /// /// /// true if should be called to /// find a common set of values the object supports; otherwise, false. /// public override bool GetStandardValuesSupported(ITypeDescriptorContext context) { return true; } /// /// Returns whether the collection of standard values returned from /// is an exclusive list of /// possible values, using the specified context. /// /// /// An that provides a format context. /// /// /// true if the returned from /// is an exhaustive list of /// possible values; false if other values are possible. /// public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) { return true; } /// /// Returns a collection of standard string values. /// /// /// An that provides a format context that /// can be used to extract additional information about the environment from which this /// converter is invoked. This parameter or properties of this parameter can be null. /// /// /// A that holds a standard set of valid values, /// or null if the data type does not support a standard set of values. /// public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { return _valuesFactory != null ? new StandardValuesCollection(_valuesFactory.Invoke()) : EmptyStandardValuesCollection; } }