dotnet/wpf · error · InvalidEnumArgumentException

SR.Format(SR.Enum_Invalid, "DesignerSerializationOptions")

Error message

SR.Format(SR.Enum_Invalid, "DesignerSerializationOptions")

What it means

DesignerSerializationOptionsAttribute's constructor validates that the value passed for designerSerializationOptions is a defined member of the DesignerSerializationOptions enum, and only SerializeAsAttribute is accepted here. If any other value (e.g. an undefined cast or default) is supplied, an InvalidEnumArgumentException with 'Enum_Invalid, DesignerSerializationOptions' is thrown. The library throws this to fail fast on enum values that have no meaning for the attribute.

Solutions

  1. Pass only DesignerSerializationOptions.SerializeAsAttribute to the constructor
  2. Cast the value to DesignerSerializationOptions and check Enum.IsDefined(typeof(DesignerSerializationOptions), value) before constructing
  3. Fix the source of the wrong value (config/int parsing) so a valid enum member arrives
  4. Reference the same WindowsBase version the value was defined in

Example fix

// before
var attr = new DesignerSerializationOptionsAttribute((DesignerSerializationOptions)7);
// after
var options = DesignerSerializationOptions.SerializeAsAttribute;
var attr = new DesignerSerializationOptionsAttribute(options);
Defensive patterns

Strategy: validation

Validate before calling

static bool IsValidDesignerSerializationOptions(DesignerSerializationOptions v) => v == DesignerSerializationOptions.SerializeAsAttribute;

Type guard

static bool IsDefinedEnum<TEnum>(object v) where TEnum : struct, Enum => Enum.IsDefined(typeof(TEnum), v);

Try / catch

try { var attr = new DesignerSerializationOptionsAttribute(options); }
catch (System.ComponentModel.InvalidEnumArgumentException ex) { log.Error($"Invalid DesignerSerializationOptions: {ex.Message}"); }

Prevention

When it happens

Trigger: Calling new DesignerSerializationOptionsAttribute((DesignerSerializationOptions)someUndefinedValue) or passing DesignerSerializationOptions default (0) or any value other than DesignerSerializationOptions.SerializeAsAttribute.

Common situations: Hand-written XAML support code or serializers constructing the attribute with an incorrectly cast int (e.g. reading an enum value from config as int), or using a value added in a newer/older WPF version that does not exist in the referenced assembly.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DesignerSerializationOptionsAttribute.cs:27

    ///     Specifies the serialization flags per property
    /// </summary>
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Method, AllowMultiple = false)]
    public sealed class DesignerSerializationOptionsAttribute : Attribute
    {
        #region Construction
        
        /// <summary>
        ///     Constructor for DesignerSerializationOptionsAttribute
        /// </summary>
        public DesignerSerializationOptionsAttribute(DesignerSerializationOptions designerSerializationOptions)
        {
            if (DesignerSerializationOptions.SerializeAsAttribute == designerSerializationOptions)
            {
                _designerSerializationOptions = designerSerializationOptions;
            }
            else
            {
                throw new InvalidEnumArgumentException(SR.Format(SR.Enum_Invalid, "DesignerSerializationOptions"));
            }
        }

        #endregion Construction

        #region Properties

        /// <summary>
        ///     DesignerSerializationOptions
        /// </summary>
        public DesignerSerializationOptions DesignerSerializationOptions
        {
            get { return _designerSerializationOptions; }
        }

        #endregion Properties

        #region Data

View on GitHub (pinned to 81131a70a4)