dotnet/wpf · error · InvalidEnumArgumentException

autoComplete

Error message

autoComplete

What it means

The TextComposition constructor (TextComposition.cs:86) throws InvalidEnumArgumentException("autoComplete", ...) when the autoComplete argument is anything other than TextCompositionAutoComplete.Off or TextCompositionAutoComplete.On. This is an eager public-API argument check to avoid Enum.IsDefined's performance and versioning pitfalls.

Solutions

  1. Pass only TextCompositionAutoComplete.On or TextCompositionAutoComplete.Off.
  2. Clamp/normalize external input: undefined values should map to Off (the safe default).
  3. Remove casts from int to TextCompositionAutoComplete in interop layers or validate them first.

Example fix

// before
var tc = new TextComposition(im, src, text, (TextCompositionAutoComplete)raw);
// after
var ac = raw == 1 ? TextCompositionAutoComplete.On : TextCompositionAutoComplete.Off;
var tc = new TextComposition(im, src, text, ac);
Defensive patterns

Strategy: validation

Validate before calling

if (autoComplete != TextCompositionAutoComplete.On && autoComplete != TextCompositionAutoComplete.Off)
    throw new ArgumentException("autoComplete must be On or Off");

Type guard

static bool IsValidAutoComplete(TextCompositionAutoComplete a) => a == TextCompositionAutoComplete.On || a == TextCompositionAutoComplete.Off;

Try / catch

try { new TextComposition(im, src, text, autoComplete); } catch (InvalidEnumArgumentException) { /* default to TextCompositionAutoComplete.Off */ }

Prevention

When it happens

Trigger: Calling new TextComposition(inputManager, source, text, autoComplete) with an out-of-range value, e.g. a cast (TextCompositionAutoComplete)5 or a value from a future/other enum version.

Common situations: Interop or reflection-based composition code passing raw ints; config-driven autocomplete settings mapped incorrectly; typo mapping a bool to an undefined enum value.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/TextComposition.cs:86

        #region Constructors

        /// <summary>
        ///     The constrcutor of TextComposition class.
        /// </summary>
        public TextComposition(InputManager inputManager, IInputElement source, string resultText) : this(inputManager, source,  resultText, TextCompositionAutoComplete.On)
        {
        }

        /// <summary>
        ///     The constrcutor of TextComposition class.
        /// </summary>
        public TextComposition(InputManager inputManager, IInputElement source, string resultText, TextCompositionAutoComplete autoComplete) : this(inputManager, source, resultText, autoComplete, InputManager.Current.PrimaryKeyboardDevice)
        {
            // We should avoid using Enum.IsDefined for performance and correct versioning. 
            if ((autoComplete != TextCompositionAutoComplete.Off) &&
                (autoComplete != TextCompositionAutoComplete.On))
            {
                throw new InvalidEnumArgumentException("autoComplete", 
                                                       (int)autoComplete, 
                                                       typeof(TextCompositionAutoComplete));
            }
        }

        //
        // An internal constructore to specify InputDevice directly.
        //
        internal TextComposition(InputManager inputManager, IInputElement source, string resultText, TextCompositionAutoComplete autoComplete, InputDevice inputDevice)
        {
            _inputManager = inputManager;

            _inputDevice = inputDevice;

            if (resultText == null)
            {
                throw new ArgumentException(SR.TextComposition_NullResultText);
            }

View on GitHub (pinned to 81131a70a4)