{"record":{"id":"06900ab603811b7b","repo":"dotnet/wpf","slug":"invalidenumargumentexception-type-int-type-typeof-inputtype","errorCode":null,"errorMessage":"InvalidEnumArgumentException(\"type\", (int)type, typeof(InputType))","messagePattern":"InvalidEnumArgumentException\\(\"type\", \\(int\\)type, typeof\\(InputType\\)\\)","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputReport.cs","lineNumber":96,"sourceCode":"        }\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;\n                default:\n                    throw new  System.ComponentModel.InvalidEnumArgumentException(\"type\", (int)type, typeof(InputType));\n            }\n        }\n\n        private readonly PresentationSource _inputSource;\n        private InputType _type;\n        private InputMode _mode;\n        private int _timestamp;\n    }\n}\n","sourceCodeStart":78,"sourceCodeEnd":106,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputReport.cs#L78-L106","documentation":"InputReport's private Validate_InputType throws System.ComponentModel.InvalidEnumArgumentException when the type parameter is not one of InputType.System, Keyboard, Mouse, Stylus, Hid, Text, or Command. The type tells the InputManager which report category is being delivered, so undefined values are rejected at construction.","triggerScenarios":"Constructing InputReport with an InputType value outside the seven defined members, e.g. an int cast like (InputType)99 or a corrupted/deserialized value.","commonSituations":"Custom raw-input provider code mapping Win32 device messages to InputType; persisted input logs replayed with mismatched enum versions; unit tests generating synthetic reports.","solutions":["Use only defined InputType members when constructing reports.","Whitelist-validate the numeric source before the cast (switch on the raw int over valid values).","Catch InvalidEnumArgumentException around construction when replaying external data, and log-and-drop the bad report."],"exampleFix":"// before\nvar report = new InputReport(source, (InputType)rawInt, mode, timestamp);\n// after\nif (rawInt < 0 || rawInt > 6) throw new InvalidDataException(\"bad input type\");\nvar report = new InputReport(source, (InputType)rawInt, mode, timestamp);","handlingStrategy":"validation","validationCode":"static readonly InputType[] ValidTypes = { InputType.System, InputType.Keyboard, InputType.Mouse, InputType.Stylus, InputType.Hid, InputType.Text, InputType.Command };\nif (!ValidTypes.Contains(type)) throw new InvalidDataException($\"Invalid InputType: {type}\");","typeGuard":"static bool IsValidInputType(InputType t) =>\n    t is InputType.System or InputType.Keyboard or InputType.Mouse\n        or InputType.Stylus or InputType.Hid or InputType.Text or InputType.Command;","tryCatchPattern":"try { var report = new InputReport(src, type, mode, ts); }\ncatch (InvalidEnumArgumentException) { /* log and skip report */ }","preventionTips":["Map Win32 device messages to InputType via an explicit switch, never unchecked casts.","Validate replayed/logged input data against the current enum definition.","Keep a helper whitelist function and use it at every construction site."],"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-21T21:30:21.729Z"}