dotnet/wpf · error · InvalidEnumArgumentException

category

Error message

category

What it means

The LocalizabilityAttribute constructor validates that the LocalizationCategory value lies between None and NeverLocalize; any out-of-range enum value throws InvalidEnumArgumentException. This prevents undefined category values from being attached to localizable members.

Solutions

  1. Validate the int against Enum.IsDefined(typeof(LocalizationCategory), value) before constructing the attribute
  2. Use a named LocalizationCategory constant instead of a raw int cast
  3. Check for enum renames/removals if code was migrated between .NET versions and map legacy values to current ones

Example fix

// before
var attr = new LocalizabilityAttribute((LocalizationCategory)999);
// after
LocalizationCategory category = (LocalizationCategory)999;
if (!Enum.IsDefined(typeof(LocalizationCategory), category))
    category = LocalizationCategory.None;
var attr = new LocalizabilityAttribute(category);
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(LocalizationCategory), value)) throw new ArgumentException("Invalid LocalizationCategory");

Type guard

static bool IsValidCategory(LocalizationCategory c) => c >= LocalizationCategory.None && c <= LocalizationCategory.NeverLocalize && Enum.IsDefined(typeof(LocalizationCategory), c);

Try / catch

try { var attr = new LocalizabilityAttribute(category); }
catch (InvalidEnumArgumentException ex) { /* use LocalizationCategory.None */ }

Prevention

When it happens

Trigger: Calling new LocalizabilityAttribute((LocalizationCategory)someInt) with an int cast that is not a defined LocalizationCategory member — common when computing the category dynamically or deserializing an int from config/data.

Common situations: Programmatically applying localizability attributes from a database or file where category codes changed between .NET/WPF versions; casting a different enum's int value by mistake; typos in generated code.

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/a99268f27923a7fa. Report an issue: GitHub.

Appendix: source

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

         | AttributeTargets.Enum     
         | AttributeTargets.Struct,
         AllowMultiple = false, 
         Inherited = true)
    ]
    public sealed class LocalizabilityAttribute : Attribute 
    {
        /// <summary>
        /// Construct a LocalizabilityAttribute to describe the localizability of a property.
        /// Modifiability property default to Modifiability.Modifiable, and Readability property
        /// default to Readability.Readable.
        /// </summary>
        /// <param name="category">the string category given to the item</param>
        public LocalizabilityAttribute(LocalizationCategory category)
        {
            if ( category < LocalizationCategory.None
              || category > LocalizationCategory.NeverLocalize)
            {
                throw new InvalidEnumArgumentException(
                    "category", 
                    (int)category, 
                    typeof(LocalizationCategory)
                    );
            }

            _category      = category;
            _readability   = Readability.Readable;
            _modifiability = Modifiability.Modifiable;
        }

      
        /// <summary>
        /// String category
        /// </summary>
        /// <value>gets or sets the string category for the item</value>
        public LocalizationCategory Category
        {

View on GitHub (pinned to 81131a70a4)