.net Custom Configuration How to case insensitive parse an enum ConfigurationProperty
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When you build a classic System.Configuration custom section in .NET, enum values often come from XML attributes. If you want those values to parse case-insensitively, the safest approach is to use a custom converter that calls Enum.Parse or Enum.TryParse with ignoreCase: true. That keeps the configuration API strongly typed while avoiding fragile casing requirements in the config file.
Why the Default Setup Can Be Painful
A typical custom configuration section exposes an enum property like this:
And the section might read from XML such as:
If your parsing path expects exact enum casing, console may fail while Console succeeds. That is unnecessary friction for configuration files.
Use a Custom ConfigurationConverterBase
Create a converter that understands your enum and parses it case-insensitively.
The important part is the third argument to Enum.TryParse: true means ignore case.
Apply the Converter to the Property
Now attach the converter to your configuration property.
With that setup, all of these can map to the same enum value:
- '
Console' - '
console' - '
CONSOLE'
Example Config File
And the read path stays strongly typed:
An Alternative: Read a String and Parse Manually
If you want the least magic, you can store the raw configuration value as a string and parse it in a separate property.
This is simpler to reason about, but you lose some of the elegance of a directly typed configuration property.
Validate Early and Fail Clearly
Whichever approach you choose, fail with a precise configuration error when the value is invalid. Configuration bugs are much cheaper to diagnose when the startup error says exactly which property and value failed, rather than surfacing later as a null or default-path behavior elsewhere in the application.
Keep the Advice Scoped to Classic Configuration
This pattern applies to the older System.Configuration model used by ConfigurationSection and ConfigurationProperty. If you are on modern Microsoft.Extensions.Configuration, the binding story is different. Mixing those two configuration systems is a common source of confusion when searching for examples.
Common Pitfalls
- Assuming enum parsing will always be case-insensitive by default.
- Throwing generic exceptions instead of a configuration-specific error.
- Hiding invalid values by silently falling back to a default without logging.
- Overcomplicating the section when a plain string plus explicit parse would be enough.
- Mixing
Microsoft.Extensions.Configurationadvice with classicSystem.Configurationcode.
Summary
- Use a custom
ConfigurationConverterBasefor case-insensitive enum parsing in classic .NET configuration sections. - '
Enum.TryParse(..., true, ...)is the key implementation detail.' - Attach the converter with
TypeConverteron the property. - A string-backed property with manual parsing is a simpler alternative.
- Keep the error message clear so invalid config values are easy to diagnose.

