Skip to content

Instantly share code, notes, and snippets.

@justinyoo
Created June 17, 2016 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinyoo/a4880148a725df8fd15dee028565c077 to your computer and use it in GitHub Desktop.
Save justinyoo/a4880148a725df8fd15dee028565c077 to your computer and use it in GitHub Desktop.
Configuration Value Converter for enum and List<T>
public class CaseInsensitiveEnumConverter<TEnum> : ConfigurationConverterBase where TEnum : struct
{
public override object ConvertFrom(ITypeDescriptorContext ctx, CultureInfo culture, object value)
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
TEnum result;
if (!Enum.TryParse((string)value, true, out result))
{
throw new InvalidOperationException("Invalid enum value");
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment