<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.4.0" />

GenericEnumConverter

using System.ComponentModel; using System.Globalization; using System.Text; namespace System.Configuration { public sealed class GenericEnumConverter : ConfigurationConverterBase { private readonly Type _enumType; public GenericEnumConverter(Type typeEnum) { if (typeEnum == (Type)null) throw new ArgumentNullException("typeEnum"); _enumType = typeEnum; } public override object ConvertTo(ITypeDescriptorContext ctx, CultureInfo ci, object value, Type type) { return value.ToString(); } public override object ConvertFrom(ITypeDescriptorContext ctx, CultureInfo ci, object data) { try { string text = (string)data; if (string.IsNullOrEmpty(text)) throw new Exception(); if (!string.IsNullOrEmpty(text) && (char.IsDigit(text[0]) || text[0] == '-' || text[0] == '+')) throw new Exception(); if (text != text.Trim()) throw new Exception(); return Enum.Parse(_enumType, text); } catch { StringBuilder stringBuilder = new StringBuilder(); string[] names = Enum.GetNames(_enumType); foreach (string value in names) { if (stringBuilder.Length != 0) stringBuilder.Append(", "); stringBuilder.Append(value); } throw new ArgumentException(string.Format(System.SR.Invalid_enum_value, stringBuilder.ToString())); } } } }