dotnet/wpf · error · InvalidEnumArgumentException

InvalidEnumArgumentException("type", (int)type…

Error message

InvalidEnumArgumentException("type", (int)type, typeof(InputType))

What it means

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.

Solutions

  1. Use only defined InputType members when constructing reports.
  2. Whitelist-validate the numeric source before the cast (switch on the raw int over valid values).
  3. Catch InvalidEnumArgumentException around construction when replaying external data, and log-and-drop the bad report.

Example fix

// before
var report = new InputReport(source, (InputType)rawInt, mode, timestamp);
// after
if (rawInt < 0 || rawInt > 6) throw new InvalidDataException("bad input type");
var report = new InputReport(source, (InputType)rawInt, mode, timestamp);
Defensive patterns

Strategy: validation

Validate before calling

static readonly InputType[] ValidTypes = { InputType.System, InputType.Keyboard, InputType.Mouse, InputType.Stylus, InputType.Hid, InputType.Text, InputType.Command };
if (!ValidTypes.Contains(type)) throw new InvalidDataException($"Invalid InputType: {type}");

Type guard

static bool IsValidInputType(InputType t) =>
    t is InputType.System or InputType.Keyboard or InputType.Mouse
        or InputType.Stylus or InputType.Hid or InputType.Text or InputType.Command;

Try / catch

try { var report = new InputReport(src, type, mode, ts); }
catch (InvalidEnumArgumentException) { /* log and skip report */ }

Prevention

When it happens

Trigger: Constructing InputReport with an InputType value outside the seven defined members, e.g. an int cast like (InputType)99 or a corrupted/deserialized value.

Common situations: Custom raw-input provider code mapping Win32 device messages to InputType; persisted input logs replayed with mismatched enum versions; unit tests generating synthetic 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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/06900ab603811b7b. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputReport.cs:96

        }

        /// <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;
                default:
                    throw new  System.ComponentModel.InvalidEnumArgumentException("type", (int)type, typeof(InputType));
            }
        }

        private readonly PresentationSource _inputSource;
        private InputType _type;
        private InputMode _mode;
        private int _timestamp;
    }
}

View on GitHub (pinned to 81131a70a4)