{"record":{"id":"65af9c7e5300ce96","repo":"dotnet/wpf","slug":"invalidenumargumentexception-mode-int-mode-typeof-inputmode","errorCode":null,"errorMessage":"InvalidEnumArgumentException(\"mode\", (int)mode, typeof(InputMode))","messagePattern":"InvalidEnumArgumentException\\(\"mode\", \\(int\\)mode, typeof\\(InputMode\\)\\)","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputReport.cs","lineNumber":76,"sourceCode":"\n        /// <summary>\n        ///     Read-only access to the time when the input occurred.\n        /// </summary>\n        public int Timestamp {get {return _timestamp;}}\n\n        /// <summary>\n        /// There is a proscription against using Enum.IsDefined().  (it is slow)\n        /// so we write these PRIVATE validate routines instead.\n        /// </summary>\n        private void Validate_InputMode( InputMode mode )\n        {\n            switch( mode )\n            {\n                case InputMode.Foreground:\n                case InputMode.Sink:\n                    break;\n                default:\n                    throw new  System.ComponentModel.InvalidEnumArgumentException(\"mode\", (int)mode, typeof(InputMode));\n            }\n        }\n\n        /// <summary>\n        /// There is a proscription against using Enum.IsDefined().  (it is slow)\n        /// so we write these PRIVATE validate routines instead.\n        /// </summary>\n        private void Validate_InputType( InputType type )\n        {\n            switch( type )\n            {\n                case InputType.Keyboard:\n                case InputType.Mouse:\n                case InputType.Stylus:\n                case InputType.Hid:\n                case InputType.Text:\n                case InputType.Command:\n                    break;","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputReport.cs#L58-L94","documentation":"InputReport's private Validate_InputMode throws System.ComponentModel.InvalidEnumArgumentException when the mode parameter is not InputMode.Foreground or InputMode.Sink. This is constructor-time argument validation because InputMode has exactly two defined members and anything else indicates a bad cast or corrupted data.","triggerScenarios":"Constructing InputReport (or a subclass like RawKeyboardInputReport) with an undefined InputMode value, e.g. (InputMode)3 or a value deserialized from untrusted data.","commonSituations":"Custom input provider code casting raw ints to InputMode; reflection/serialization round-trips that produce out-of-range enum values; tests feeding synthetic input reports.","solutions":["Only pass InputMode.Foreground or InputMode.Sink when constructing InputReport instances.","Validate numeric sources before casting: if (n != (int)InputMode.Foreground && n != (int)InputMode.Sink) throw/reject.","Wrap construction in try-catch for InvalidEnumArgumentException when handling external input data."],"exampleFix":"// before\nvar report = new InputReport(source, type, (InputMode)rawInt, timestamp);\n// after\nvar mode = rawInt == (int)InputMode.Sink ? InputMode.Sink : InputMode.Foreground;\nvar report = new InputReport(source, type, mode, timestamp);","handlingStrategy":"validation","validationCode":"if (mode is not (InputMode.Foreground or InputMode.Sink))\n    throw new InvalidDataException($\"Invalid InputMode: {mode}\");","typeGuard":"static bool IsValidInputMode(InputMode mode) =>\n    mode is InputMode.Foreground or InputMode.Sink;","tryCatchPattern":"try { var report = new InputReport(src, type, mode, ts); }\ncatch (InvalidEnumArgumentException) { /* log and drop bad report */ }","preventionTips":["Whitelist raw ints against defined InputMode values before casting.","Never deserialize InputMode without validation.","Add Debug.Assert(IsValidInputMode(mode)) in custom provider code during development."],"tags":["wpf","input","enum","invalid-argument"],"backgroundTag":"invalid-enum-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}