-
-
Save justinyoo/a4880148a725df8fd15dee028565c077 to your computer and use it in GitHub Desktop.
Configuration Value Converter for enum and List<T>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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