dotnet/wpf · error · ArgumentException

throw new…

Error message

throw new ArgumentException(SR.Format(SR.GenericInvalidArgument, argName));

What it means

AutomationInteropProvider.ThrowInvalidArgument throws a generic ArgumentException(SR.GenericInvalidArgument, argName) when RaiseAutomationEvent validation fails. It signals that one of the arguments passed to RaiseAutomationEvent (typically the event identifier or runtime element) is invalid per the private validation checks.

Solutions

  1. Inspect the argName in the exception message to identify which argument is invalid
  2. Verify the AutomationEvent was obtained via AutomationEvent.Register (or PatternEvent) and not constructed ad hoc
  3. Ensure the source element is a valid, live AutomationPeer/RawElementProvider before raising the event
  4. Add null checks for eventId, source, and args before calling RaiseAutomationEvent

Example fix

// before
AutomationInteropProvider.RaiseAutomationEvent(myEvent, peer, args);
// after
if (myEvent == null || peer == null || args == null)
    throw new ArgumentNullException(nameof(myEvent));
AutomationInteropProvider.RaiseAutomationEvent(myEvent, peer, args);
Defensive patterns

Strategy: validation

Validate before calling

if (eventId == null || source == null || args == null)
    throw new ArgumentNullException(nameof(eventId));
// eventId must come from AutomationEvent.Register / PatternEvent

Type guard

bool IsValidEvent(AutomationEvent e) => e != null && e.Id != 0;

Try / catch

try { AutomationInteropProvider.RaiseAutomationEvent(evt, peer, args); }
catch (ArgumentException ex) { log.Error($"Invalid argument to RaiseAutomationEvent: {ex.ParamName}"); }

Prevention

When it happens

Trigger: Calling AutomationInteropProvider.RaiseAutomationEvent with an argument that fails ValidateArgument/ValidateCondition checks — e.g. a null or invalid AutomationEvent/runtimeId/element combination.

Common situations: WPF/WinForms custom control authors raising UIA events from provider code; passing an event object obtained from a different provider or a disposed element.

Understand the failure class

Background: "must be a positive integer", "cannot be empty", "invalid argument": how invalid-argument errors work across open-source libraries — this error's family across 33 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationProvider/System/Windows/Automation/Provider/AutomationInteropProvider.cs:182

            ArgumentNullException.ThrowIfNull(e);

            UiaCoreProviderApi.UiaRaiseStructureChangedEvent(provider, e.StructureChangeType, e.GetRuntimeId());
        }
        #endregion Public Methods


        //------------------------------------------------------
        //
        //  Private Methods
        //
        //------------------------------------------------------

        #region Private Methods

        // Throw an argument Exception with a generic error
        private static void ThrowInvalidArgument(string argName)
        {
            throw new ArgumentException(SR.Format(SR.GenericInvalidArgument, argName));
        }

        // Check that specified condition is true; if not, throw exception
        private static void ValidateArgument(bool cond, string reason)
        {
            if (!cond)
            {
                throw new ArgumentException(SR.GetResourceString(reason, null));
            }
        }

        #endregion Private Methods
    }
}

View on GitHub (pinned to 81131a70a4)