/// <summary>Converts the given value object to the specified destination type.</summary> /// <param name="context">An <see cref="ITypeDescriptorContext"></see> that provides a format context.</param> /// <param name="culture">An optional <see cref="CultureInfo"></see>. If not supplied, the current /// culture is assumed.</param> /// <param name="value">The <see cref="object"></see> to convert.</param> /// <param name="destinationType">The <see cref="Type"></see> to convert the value to.</param> /// <returns>An <see cref="object"></see> that represents the converted <paramref name="value">value</paramref> /// </returns> /// <exception cref="ArgumentNullException"><paramref name="destinationType">destinationType</paramref> is null. /// </exception> /// <exception cref="ArgumentException"><paramref name="value">value</paramref> is not a valid value /// for the enumeration.</exception> /// <exception cref="NotSupportedException">The conversion cannot be performed.</exception> public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (value?.GetType().IsSubclassOf(typeof(EnumerationType)) == true && destinationType == typeof(string)) { return(((EnumerationType)value).DisplayName); } return(value is string valueString && destinationType == typeof(string) ? ((EnumerationType)EnumerationType.FromDisplayName(EnumType, valueString)).DisplayName : base.ConvertTo(context, culture, value, destinationType)); }
/// <summary>Converts the specified value object to an enumeration object.</summary> /// <param name="context">An <see cref="ITypeDescriptorContext"></see> that provides a format context.</param> /// <param name="culture">An optional <see cref="CultureInfo"></see>. If not supplied, the current /// culture is assumed.</param> /// <param name="value">The <see cref="object"></see> to convert.</param> /// <returns>An <see cref="object"></see> that represents the converted <paramref name="value">value</paramref> /// .</returns> /// <exception cref="FormatException"><paramref name="value">value</paramref> is not a valid value /// for the target type.</exception> /// <exception cref="NotSupportedException">The conversion cannot be performed.</exception> public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string valueString) { return(EnumerationType.FromDisplayName(EnumType, valueString)); } if (value?.GetType().IsSubclassOf(typeof(EnumerationType)) == true) { return((EnumerationType)value); } return(base.ConvertFrom(context, culture, value)); }
/// <summary>Initializes a new instance of the <see cref="EnumConverter"></see> class for the given type.</summary> /// <param name="type">A <see cref="Type"></see> that represents the type of enumeration to associate /// with this enumeration converter.</param> public EnumerationTypeConverter(Type type) : base(type) { if (type is null) { throw new ArgumentNullException(nameof(type)); } if (!type.IsSubclassOf(typeof(EnumerationType))) { throw new ArgumentException(ErrorMessageResources.EnumerationTypeDerivedClassExpected, nameof(type)); } Values = new StandardValuesCollection( (EnumerationType.GetAll(type).Cast <EnumerationType>()) .Select(s => s.DisplayName) .ToList()); }