dotnet/wpf · error · InvalidEnumArgumentException

InvalidEnumArgumentException("action", (int)action…

Error message

InvalidEnumArgumentException("action", (int)action, typeof(AnnotationAction))

What it means

The AnnotationAuthorChangedEventArgs constructor throws InvalidEnumArgumentException when the action parameter is outside the valid AnnotationAction range (Added..Modified). This guards event args used for AuthorChanged events so subscribers always receive a defined action value.

Solutions

  1. Pass only defined AnnotationAction members (Added, Removed, Modified) when constructing the args.
  2. Validate with Enum.IsDefined(typeof(AnnotationAction), value) before constructing.
  3. Fix unchecked int-to-AnnotationAction casts in custom store code.
  4. Audit persisted action codes and map them to valid current enum members.

Example fix

// before
var args = new AnnotationAuthorChangedEventArgs(annotation, (AnnotationAction)code, author);

// after
if (!Enum.IsDefined(typeof(AnnotationAction), code)) throw new InvalidDataException("bad action code");
var args = new AnnotationAuthorChangedEventArgs(annotation, (AnnotationAction)code, author);
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(AnnotationAction), action))
    throw new InvalidDataException($"Invalid AnnotationAction: {action}");
var args = new AnnotationAuthorChangedEventArgs(annotation, action, author);

Type guard

bool IsValidAuthorAction(AnnotationAction a) =>
    a is AnnotationAction.Added or AnnotationAction.Removed or AnnotationAction.Modified;

Try / catch

try { RaiseAuthorChanged(new AnnotationAuthorChangedEventArgs(a, act, author)); }
catch (InvalidEnumArgumentException ex) { log(ex); }

Prevention

When it happens

Trigger: Constructing AnnotationAuthorChangedEventArgs with an undefined AnnotationAction value, e.g. from an unchecked int cast ((AnnotationAction)42), a default-initialized field, or computed enum arithmetic producing an out-of-range value.

Common situations: Custom AnnotationStore implementations raising AuthorChanged events with bad enum values; deserializing persisted int action codes without validation; refactorings that changed enum members leaving stale numeric values.

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/679ec5643b0d110a. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/AnnotationAuthorChangedEventArgs.cs:56

        #region Constructors

        /// <summary>
        ///     Creates an instance of AuthorChangedEventArgs.
        /// </summary>
        /// <param name="annotation">the Annotation firing the event</param>
        /// <param name="action">the action taken on the Author</param>
        /// <param name="author">the Author that was changed</param>
        /// <exception cref="ArgumentNullException">annotation is null</exception>
        /// <exception cref="InvalidEnumArgumentException">action is not a valid value from AnnotationAction</exception>
        public AnnotationAuthorChangedEventArgs(Annotation annotation, AnnotationAction action, Object author)
        {
            // The author parameter can be null here - it is possible to add a null to
            // the list of authors and we must fire an event signalling a change in the collection.

            ArgumentNullException.ThrowIfNull(annotation);
            if (action < AnnotationAction.Added || action > AnnotationAction.Modified)
            {
                throw new InvalidEnumArgumentException("action", (int)action, typeof(AnnotationAction));
            }

            _annotation = annotation;
            _author = author;
            _action = action;
        }

        #endregion Constructors

        //------------------------------------------------------
        //
        //  Public Properties
        //
        //------------------------------------------------------

        #region Public Properties

        /// <summary>

View on GitHub (pinned to 81131a70a4)