dotnet/wpf · error · InvalidEnumArgumentException

Readability

Error message

Readability

What it means

The Readability property setter of LocalizabilityAttribute accepts only Readability.Unreadable, Readable, or Inherit; any other value throws InvalidEnumArgumentException. This restricts the property to the three defined readability levels.

Solutions

  1. Whitelist-check the value against Enum.IsDefined(typeof(Readability), value) and the three valid members before assigning
  2. Assign named constants (Readability.Readable / Unreadable / Inherit) instead of casts
  3. Fix cross-enum assignment bugs where a wrong enum's value is passed to the setter

Example fix

// before
attr.Readability = (Readability)modifiabilityValue; // wrong enum
// after
Readability r = (Readability)rawValue;
attr.Readability = (r == Readability.Readable || r == Readability.Unreadable || r == Readability.Inherit)
    ? r : Readability.Inherit;
Defensive patterns

Strategy: validation

Validate before calling

if (value != Readability.Unreadable && value != Readability.Readable && value != Readability.Inherit) throw new ArgumentException("Invalid Readability");

Type guard

static bool IsValidReadability(Readability r) => r is Readability.Unreadable or Readability.Readable or Readability.Inherit;

Try / catch

try { attr.Readability = value; }
catch (InvalidEnumArgumentException) { attr.Readability = Readability.Inherit; }

Prevention

When it happens

Trigger: Assigning attribute.Readability = (Readability)otherInt with an undefined int, or assigning a value from a different enum (e.g. a LocalizationCategory or Modifiability value) to Readability.

Common situations: Reflection-driven or generated code mapping legacy readability codes; copy-paste errors where a Modifiability value is assigned to Readability; config-driven attribute construction with invalid ints.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/20ff17c5d0f68653. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/LocalizabilityAttribute.cs:76

        {
            // should have only getter, because it is a required parameter to the constructor
            get { return _category; }         
        }      

        /// <summary>
        /// Get or set the readability of the attribute's targeted value
        /// </summary>
        /// <value>Readability</value>
        public Readability Readability
        {
            get { return _readability; }
            set 
            { 
                if (  value != Readability.Unreadable 
                   && value != Readability.Readable 
                   && value != Readability.Inherit)
                {
                    throw new InvalidEnumArgumentException("Readability", (int) value, typeof(Readability));
                }

                _readability = value;
            }
        }

        /// <summary>
        /// Get or set the modifiability of the attribute's targeted value
        /// </summary>
        /// <value>Modifiability</value>
        public Modifiability Modifiability
        {
            get { return _modifiability; }
            set 
            {
                if (   value != Modifiability.Unmodifiable
                    && value != Modifiability.Modifiable
                    && value != Modifiability.Inherit)

View on GitHub (pinned to 81131a70a4)