{"record":{"id":"23122cd14ff9fb06","repo":"dotnet/wpf","slug":"invalidenumargumentexception-action-int-action-typeof-23122c","errorCode":null,"errorMessage":"InvalidEnumArgumentException(\"action\", (int)action, typeof(AnnotationAction))","messagePattern":"InvalidEnumArgumentException\\(\"action\", \\(int\\)action, typeof\\(AnnotationAction\\)\\)","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/AnnotationResourceChangedEventArgs.cs","lineNumber":56,"sourceCode":"        #region Constructors\n\n        /// <summary>\n        ///     Creates an instance of AnnotationResourceChangedEventArgs.\n        /// </summary>\n        /// <param name=\"annotation\">the Annotation firing the event</param>\n        /// <param name=\"action\">the action taken on the Resource</param>\n        /// <param name=\"resource\">the Resource that was changed</param>\n        /// <exception cref=\"ArgumentNullException\">annotation or action is null</exception>\n        /// <exception cref=\"InvalidEnumArgumentException\">action is not a valid value from AnnotationAction</exception>\n        public AnnotationResourceChangedEventArgs(Annotation annotation, AnnotationAction action, AnnotationResource resource)\n        {\n            // The resource parameter can be null here - it is possible to add a null to\n            // the list of resources and we must fire an event signalling a change in the collection.\n\n            ArgumentNullException.ThrowIfNull(annotation);\n            if (action < AnnotationAction.Added || action > AnnotationAction.Modified)\n            {\n                throw new InvalidEnumArgumentException(\"action\", (int)action, typeof(AnnotationAction));\n            }\n\n            _annotation = annotation;\n            _resource = resource;\n            _action = action;\n        }\n\n        #endregion Constructors\n\n        //------------------------------------------------------\n        //\n        //  Public Properties\n        //\n        //------------------------------------------------------\n\n        #region Public Properties\n\n        /// <summary>","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/AnnotationResourceChangedEventArgs.cs#L38-L74","documentation":"The AnnotationResourceChangedEventArgs constructor throws InvalidEnumArgumentException when the action parameter is not one of the defined AnnotationAction values within the valid range (Added through Modified). Callers firing annotation change events must pass a valid AnnotationAction; out-of-range values indicate a coding error in event-raising code.","triggerScenarios":"Constructing AnnotationResourceChangedEventArgs with an int or AnnotationAction value outside the Added..Modified range (e.g. (AnnotationAction)99, uninitialized/default enum from unchecked casts, or arithmetic on enum values).","commonSituations":"Custom AnnotationStore implementations raising StoreContentChanged events with improperly cast enum values; enum values persisted as int and deserialized without validation; new enum members added upstream but not handled by old code paths.","solutions":["Pass only valid AnnotationAction values (Added, Removed, Modified) when constructing the args.","Validate the enum value before constructing: if (!Enum.IsDefined(typeof(AnnotationAction), action)) ...","Fix int-to-enum casts that produce out-of-range values from persisted data.","Update custom store/event-raising code to map legacy action codes to current enum members."],"exampleFix":"// before\nvar args = new AnnotationResourceChangedEventArgs(annotation, (AnnotationAction)rawValue, resource);\n\n// after\nvar action = Enum.IsDefined(typeof(AnnotationAction), rawValue) ? (AnnotationAction)rawValue : AnnotationAction.Modified;\nvar args = new AnnotationResourceChangedEventArgs(annotation, action, resource);","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(AnnotationAction), action) || action < AnnotationAction.Added || action > AnnotationAction.Modified)\n    action = AnnotationAction.Modified; // or reject\nvar args = new AnnotationResourceChangedEventArgs(annotation, action, resource);","typeGuard":"bool IsValidAnnotationAction(AnnotationAction a) =>\n    a is AnnotationAction.Added or AnnotationAction.Removed or AnnotationAction.Modified;","tryCatchPattern":"try { RaiseResourceChanged(new AnnotationResourceChangedEventArgs(a, act, r)); }\ncatch (InvalidEnumArgumentException ex) { log(ex); }","preventionTips":["Never cast raw ints to AnnotationAction without Enum.IsDefined.","Validate persisted enum codes on deserialization.","Keep custom store code in sync with the AnnotationAction enum."],"tags":["wpf","annotations","enum","argument-validation"],"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"}