{"record":{"id":"a99268f27923a7fa","repo":"dotnet/wpf","slug":"category","errorCode":null,"errorMessage":"category","messagePattern":"category","errorType":"validation","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/LocalizabilityAttribute.cs","lineNumber":40,"sourceCode":"         | AttributeTargets.Enum     \n         | AttributeTargets.Struct,\n         AllowMultiple = false, \n         Inherited = true)\n    ]\n    public sealed class LocalizabilityAttribute : Attribute \n    {\n        /// <summary>\n        /// Construct a LocalizabilityAttribute to describe the localizability of a property.\n        /// Modifiability property default to Modifiability.Modifiable, and Readability property\n        /// default to Readability.Readable.\n        /// </summary>\n        /// <param name=\"category\">the string category given to the item</param>\n        public LocalizabilityAttribute(LocalizationCategory category)\n        {\n            if ( category < LocalizationCategory.None\n              || category > LocalizationCategory.NeverLocalize)\n            {\n                throw new InvalidEnumArgumentException(\n                    \"category\", \n                    (int)category, \n                    typeof(LocalizationCategory)\n                    );\n            }\n\n            _category      = category;\n            _readability   = Readability.Readable;\n            _modifiability = Modifiability.Modifiable;\n        }\n\n      \n        /// <summary>\n        /// String category\n        /// </summary>\n        /// <value>gets or sets the string category for the item</value>\n        public LocalizationCategory Category\n        {","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/LocalizabilityAttribute.cs#L22-L58","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Validate the int against Enum.IsDefined(typeof(LocalizationCategory), value) before constructing the attribute","Use a named LocalizationCategory constant instead of a raw int cast","Check for enum renames/removals if code was migrated between .NET versions and map legacy values to current ones"],"exampleFix":"// before\nvar attr = new LocalizabilityAttribute((LocalizationCategory)999);\n// after\nLocalizationCategory category = (LocalizationCategory)999;\nif (!Enum.IsDefined(typeof(LocalizationCategory), category))\n    category = LocalizationCategory.None;\nvar attr = new LocalizabilityAttribute(category);","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(LocalizationCategory), value)) throw new ArgumentException(\"Invalid LocalizationCategory\");","typeGuard":"static bool IsValidCategory(LocalizationCategory c) => c >= LocalizationCategory.None && c <= LocalizationCategory.NeverLocalize && Enum.IsDefined(typeof(LocalizationCategory), c);","tryCatchPattern":"try { var attr = new LocalizabilityAttribute(category); }\ncatch (InvalidEnumArgumentException ex) { /* use LocalizationCategory.None */ }","preventionTips":["Use named enum constants instead of int casts","Check Enum.IsDefined before casting ints from config/data to the enum","Review enum diffs when migrating .NET versions"],"tags":["wpf","localization","enum","argument"],"backgroundTag":"invalid-enum-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}