dotnet/wpf · error · System.ComponentModel.InvalidEnumArgumentException

origin

Error message

origin

What it means

ByteStream.Seek validates the origin parameter with a switch over SeekOrigin; any value outside Begin/Current/End falls into the default case and throws InvalidEnumArgumentException naming "origin". Note the message is just the parameter name because the exception carries the argument details.

Solutions

  1. Pass only SeekOrigin.Begin, SeekOrigin.Current, or SeekOrigin.End literals.
  2. Validate the integer before casting: if (Enum.IsDefined(typeof(SeekOrigin), value)).
  3. Fix config/interop data to contain only 0, 1, or 2.

Example fix

// before
stream.Seek(off, (SeekOrigin)rawOrigin); // throws for rawOrigin not in {0,1,2}
// after
var origin = Enum.IsDefined(typeof(SeekOrigin), rawOrigin) ? (SeekOrigin)rawOrigin : SeekOrigin.Begin;
stream.Seek(off, origin);
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(SeekOrigin), origin)) throw new InvalidEnumArgumentException(nameof(origin), (int)origin, typeof(SeekOrigin));

Type guard

bool IsValidSeekOrigin(int v) => v is >= 0 and <= 2;

Try / catch

try { stream.Seek(off, origin); }
catch (System.ComponentModel.InvalidEnumArgumentException) { origin = SeekOrigin.Begin; }

Prevention

When it happens

Trigger: Calling Seek with an invalid cast value, e.g. (SeekOrigin)99, or passing a variable loaded from config/interop data that is not one of the three defined SeekOrigin values.

Common situations: Deserializing seek origin from persisted settings or P/Invoke-marshaled integers; third-party code passing int values into SeekOrigin casts.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/ByteStream.cs:215

            {
                case SeekOrigin.Begin:
                    translatedSeekOrigin = NativeMethods.STREAM_SEEK_SET;
                    if (0 > offset)
                    {
                        throw new ArgumentOutOfRangeException(nameof(offset),
                                                              SR.SeekNegative);
                    }
                    break;

                case SeekOrigin.Current:
                    translatedSeekOrigin = NativeMethods.STREAM_SEEK_CUR;
                    break;

                case SeekOrigin.End:
                    translatedSeekOrigin = NativeMethods.STREAM_SEEK_END;
                    break;
                default:
                    throw new System.ComponentModel.InvalidEnumArgumentException("origin",
                                                                                 (int)origin,
                                                                                 typeof(SeekOrigin));
            }

            _securitySuppressedIStream.Seek(offset, translatedSeekOrigin, out seekPos);

            return seekPos;
        }

        /// <summary>
        /// Sets the length of the current stream.
        /// 
        /// Not Supported in this implementation.
        /// </summary>
        /// <param name="newLength">New length</param>
        public override void SetLength(long newLength)
        {
            throw new NotSupportedException(SR.SetLengthNotSupported);

View on GitHub (pinned to 81131a70a4)