{"record":{"id":"dbdd2833467c1858","repo":"dotnet/wpf","slug":"sr-format-sr-enum-invalid-designerserializationoptions","errorCode":null,"errorMessage":"SR.Format(SR.Enum_Invalid, \"DesignerSerializationOptions\")","messagePattern":"SR\\.Format\\(SR\\.Enum_Invalid, \"DesignerSerializationOptions\"\\)","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DesignerSerializationOptionsAttribute.cs","lineNumber":27,"sourceCode":"    ///     Specifies the serialization flags per property\n    /// </summary>\n    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Method, AllowMultiple = false)]\n    public sealed class DesignerSerializationOptionsAttribute : Attribute\n    {\n        #region Construction\n        \n        /// <summary>\n        ///     Constructor for DesignerSerializationOptionsAttribute\n        /// </summary>\n        public DesignerSerializationOptionsAttribute(DesignerSerializationOptions designerSerializationOptions)\n        {\n            if (DesignerSerializationOptions.SerializeAsAttribute == designerSerializationOptions)\n            {\n                _designerSerializationOptions = designerSerializationOptions;\n            }\n            else\n            {\n                throw new InvalidEnumArgumentException(SR.Format(SR.Enum_Invalid, \"DesignerSerializationOptions\"));\n            }\n        }\n\n        #endregion Construction\n\n        #region Properties\n\n        /// <summary>\n        ///     DesignerSerializationOptions\n        /// </summary>\n        public DesignerSerializationOptions DesignerSerializationOptions\n        {\n            get { return _designerSerializationOptions; }\n        }\n\n        #endregion Properties\n\n        #region Data","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DesignerSerializationOptionsAttribute.cs#L9-L45","documentation":"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.","triggerScenarios":"Calling new DesignerSerializationOptionsAttribute((DesignerSerializationOptions)someUndefinedValue) or passing DesignerSerializationOptions default (0) or any value other than DesignerSerializationOptions.SerializeAsAttribute.","commonSituations":"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.","solutions":["Pass only DesignerSerializationOptions.SerializeAsAttribute to the constructor","Cast the value to DesignerSerializationOptions and check Enum.IsDefined(typeof(DesignerSerializationOptions), value) before constructing","Fix the source of the wrong value (config/int parsing) so a valid enum member arrives","Reference the same WindowsBase version the value was defined in"],"exampleFix":"// before\nvar attr = new DesignerSerializationOptionsAttribute((DesignerSerializationOptions)7);\n// after\nvar options = DesignerSerializationOptions.SerializeAsAttribute;\nvar attr = new DesignerSerializationOptionsAttribute(options);","handlingStrategy":"validation","validationCode":"static bool IsValidDesignerSerializationOptions(DesignerSerializationOptions v) => v == DesignerSerializationOptions.SerializeAsAttribute;","typeGuard":"static bool IsDefinedEnum<TEnum>(object v) where TEnum : struct, Enum => Enum.IsDefined(typeof(TEnum), v);","tryCatchPattern":"try { var attr = new DesignerSerializationOptionsAttribute(options); }\ncatch (System.ComponentModel.InvalidEnumArgumentException ex) { log.Error($\"Invalid DesignerSerializationOptions: {ex.Message}\"); }","preventionTips":["Never pass raw ints cast to the enum; use the named member SerializeAsAttribute","Validate with Enum.IsDefined before constructing attributes","Keep XAML/serializer tooling and WindowsBase versions aligned"],"tags":["wpf","invalid-enum-value","constructor"],"backgroundTag":"invalid-enum-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}