dotnet/wpf · error · InvalidEnumArgumentException
InvalidEnumArgumentException("mode", (int)mode…
Error message
InvalidEnumArgumentException("mode", (int)mode, typeof(InputMode)) What it means
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.
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.
Example fix
// before var report = new InputReport(source, type, (InputMode)rawInt, timestamp); // after var mode = rawInt == (int)InputMode.Sink ? InputMode.Sink : InputMode.Foreground; var report = new InputReport(source, type, mode, timestamp);
Defensive patterns
Strategy: validation
Validate before calling
if (mode is not (InputMode.Foreground or InputMode.Sink))
throw new InvalidDataException($"Invalid InputMode: {mode}"); Type guard
static bool IsValidInputMode(InputMode mode) =>
mode is InputMode.Foreground or InputMode.Sink; Try / catch
try { var report = new InputReport(src, type, mode, ts); }
catch (InvalidEnumArgumentException) { /* log and drop bad report */ } Prevention
- 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.
When it happens
Trigger: Constructing InputReport (or a subclass like RawKeyboardInputReport) with an undefined InputMode value, e.g. (InputMode)3 or a value deserialized from untrusted data.
Common situations: 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.
Understand the failure class
Background: "invalid argument", "unknown mode", "not supported": invalid enum-like argument errors explained — this error's family across 19 libraries.
Related errors
- InvalidEnumArgumentException("type", (int)type…
- Animation_UnrecognizedHandoffBehavior
- InvalidEnumArgumentException("action", (int)action…
- InvalidEnumArgumentException("actions", (int)actions…
- InvalidEnumArgumentException("actions", (int)actions…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/65af9c7e5300ce96.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputReport.cs:76
/// <summary>
/// Read-only access to the time when the input occurred.
/// </summary>
public int Timestamp {get {return _timestamp;}}
/// <summary>
/// There is a proscription against using Enum.IsDefined(). (it is slow)
/// so we write these PRIVATE validate routines instead.
/// </summary>
private void Validate_InputMode( InputMode mode )
{
switch( mode )
{
case InputMode.Foreground:
case InputMode.Sink:
break;
default:
throw new System.ComponentModel.InvalidEnumArgumentException("mode", (int)mode, typeof(InputMode));
}
}
/// <summary>
/// There is a proscription against using Enum.IsDefined(). (it is slow)
/// so we write these PRIVATE validate routines instead.
/// </summary>
private void Validate_InputType( InputType type )
{
switch( type )
{
case InputType.Keyboard:
case InputType.Mouse:
case InputType.Stylus:
case InputType.Hid:
case InputType.Text:
case InputType.Command:
break;View on GitHub (pinned to 81131a70a4)