{"record":{"id":"b1e556cff458ac70","repo":"dotnet/wpf","slug":"invalidenumargumentexception-action-int-action-typeof","errorCode":null,"errorMessage":"InvalidEnumArgumentException(\"action\", (int)action, typeof(RawUIStateActions))","messagePattern":"InvalidEnumArgumentException\\(\"action\", \\(int\\)action, typeof\\(RawUIStateActions\\)\\)","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/RawUIStateInputReport.cs","lineNumber":38,"sourceCode":"        /// </param>\n        /// <param name=\"timestamp\">\n        ///     The time when the input occurred.\n        /// </param>\n        /// <param name=\"action\">\n        ///     The action being reported.\n        /// </param>\n        /// <param name=\"targets\">\n        ///     The targets being reported.\n        /// </param>\n        public RawUIStateInputReport(\n            PresentationSource inputSource,\n            InputMode mode,\n            int timestamp,\n            RawUIStateActions action,\n            RawUIStateTargets targets) : base(inputSource, InputType.Keyboard, mode, timestamp)\n        {\n            if (!IsValidRawUIStateAction(action))\n                throw new System.ComponentModel.InvalidEnumArgumentException(\"action\", (int)action, typeof(RawUIStateActions));\n            if (!IsValidRawUIStateTargets(targets))\n                throw new System.ComponentModel.InvalidEnumArgumentException(\"targets\", (int)targets, typeof(RawUIStateTargets));\n\n            _action = action;\n            _targets = targets;\n        }\n\n        /// <summary>\n        ///     Read-only access to the action that was reported.\n        /// </summary>\n        public RawUIStateActions Action {get {return _action;}}\n\n        /// <summary>\n        ///     Read-only access to the targets that were reported.\n        /// </summary>\n        public RawUIStateTargets Targets {get {return _targets;}}\n\n        // IsValid Method for RawUIStateActions.","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/RawUIStateInputReport.cs#L20-L56","documentation":"The RawUIStateInputReport constructor validates the action parameter and throws InvalidEnumArgumentException when it is not a defined RawUIStateActions member. RawUIStateActions describes keyboard focus/捕捉 UI-state operations, and the constructor rejects undefined values before storing them.","triggerScenarios":"Constructing RawUIStateInputReport with an undefined RawUIStateActions value — e.g. casting an int from persisted state, passing 0, or using flags that are not one of the defined actions.","commonSituations":"Custom input-report synthesis in automation or accessibility tooling; deserializing old/foreign state files into the enum; typos when hand-writing enum combinations.","solutions":["Pass only defined RawUIStateActions members (use named values, not numeric casts).","Pre-validate with Enum.IsDefined(typeof(RawUIStateActions), action).","Fix deserialization to map legacy integers to current enum members.","Compile against the same WPF version used at runtime to avoid enum drift."],"exampleFix":"// before\nvar report = new RawUIStateInputReport(src, mode, ts, (RawUIStateActions)n, targets);\n\n// after\nvar action = (RawUIStateActions)n;\nif (!IsValidRawUIStateAction(action))\n    action = RawUIStateActions.SetFocus; // map to a defined value\nvar report = new RawUIStateInputReport(src, mode, ts, action, targets);","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(RawUIStateActions), action))\n    action = RawUIStateActions.SetFocus; // substitute a defined value","typeGuard":"bool TryGetUIStateAction(int raw, out RawUIStateActions action)\n{\n    action = (RawUIStateActions)raw;\n    return Enum.IsDefined(typeof(RawUIStateActions), action);\n}","tryCatchPattern":"try\n{\n    var report = new RawUIStateInputReport(src, mode, ts, action, targets);\n}\ncatch (InvalidEnumArgumentException ex)\n{\n    Log($\"Invalid RawUIStateActions: {ex}\");\n}","preventionTips":["Use named RawUIStateActions values only.","Validate deserialized integers against the current enum definition.","Compile and run against the same WPF version.","Cover all synthesized reports with unit tests."],"tags":["wpf","input","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"}