{"record":{"id":"5a542e517e774c4e","repo":"dotnet/wpf","slug":"invalidenumargumentexception-value-int-value-typeof-5a542e","errorCode":null,"errorMessage":"InvalidEnumArgumentException(\"value\", (int)value, typeof(XamlWriterMode))","messagePattern":"InvalidEnumArgumentException\\(\"value\", \\(int\\)value, typeof\\(XamlWriterMode\\)\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlDesignerSerializationManager.cs","lineNumber":73,"sourceCode":"        #region Properties\n\n        /// <summary>\n        ///     The mode of serialization for \n        ///     all Expressions\n        /// </summary>\n        public XamlWriterMode XamlWriterMode\n        {\n            get\n            {\n                return _xamlWriterMode;\n            }\n\n            set\n            {\n                // Validate Input Arguments\n                if (!IsValidXamlWriterMode(value)) \n                {\n                    throw new InvalidEnumArgumentException(\"value\", (int)value, typeof(XamlWriterMode));\n                }\n\n                _xamlWriterMode = value;\n            }\n        }\n\n        /// <summary>\n        ///     XmlWriter\n        /// </summary>\n        internal XmlWriter XmlWriter\n        {\n            get { return _xmlWriter; }\n        }\n\n        #endregion Properties\n\n        #region Internal Methods\n","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlDesignerSerializationManager.cs#L55-L91","documentation":"XamlDesignerSerializationManager.XamlWriterMode validates the assigned value with IsValidXamlWriterMode and throws InvalidEnumArgumentException when the value is not a defined XamlWriterMode member, preventing designers from entering an unknown serialization mode.","triggerScenarios":"Setting XamlDesignerSerializationManager.XamlWriterMode to an undefined XamlWriterMode value, e.g. an invalid cast from int or a value from another enum.","commonSituations":"Custom designer tooling casting raw ints to XamlWriterMode; storing the mode in config and converting without validation; copy-paste from code using a different enum.","solutions":["Only assign values from the XamlWriterMode enum (e.g. XamlWriterMode.Expression)","Validate raw int values against Enum.IsDefined(typeof(XamlWriterMode), v) before casting","Fix config parsing so the stored mode maps to a defined enum member"],"exampleFix":"// before\nmanager.XamlWriterMode = (XamlWriterMode)42;\n// after\nXamlWriterMode mode;\nif (Enum.TryParse(\"Expression\", out mode))\n    manager.XamlWriterMode = mode;","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(XamlWriterMode), value))\n    throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(XamlWriterMode));","typeGuard":"bool IsValidXamlWriterMode(XamlWriterMode m) => Enum.IsDefined(typeof(XamlWriterMode), m);","tryCatchPattern":"try { manager.XamlWriterMode = mode; }\ncatch (InvalidEnumArgumentException ex) { /* use a defined XamlWriterMode member */ }","preventionTips":["Assign only named XamlWriterMode members, never raw ints","Use Enum.IsDefined/Enum.TryParse before casting persisted values","Keep enum types consistent across designer tooling"],"tags":["wpf","xaml","enum","invalid-argument"],"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"}