{"record":{"id":"4c15134a95f6a86f","repo":"dotnet/wpf","slug":"invalidenumargumentexception-nameof-value-int-mouseaction","errorCode":null,"errorMessage":"InvalidEnumArgumentException(nameof(value), (int)mouseAction, typeof(MouseAction))","messagePattern":"InvalidEnumArgumentException\\(nameof\\(value\\), \\(int\\)mouseAction, typeof\\(MouseAction\\)\\)","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs","lineNumber":100,"sourceCode":"        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)\n        {\n            ArgumentNullException.ThrowIfNull(destinationType);\n\n            if (value is null || destinationType != typeof(string))\n                throw GetConvertToException(value, destinationType);\n\n            MouseAction mouseAction = (MouseAction)value;\n            return mouseAction switch\n            {\n                MouseAction.None => string.Empty,\n                MouseAction.LeftClick => \"LeftClick\",\n                MouseAction.RightClick => \"RightClick\",\n                MouseAction.MiddleClick => \"MiddleClick\",\n                MouseAction.WheelClick => \"WheelClick\",\n                MouseAction.LeftDoubleClick => \"LeftDoubleClick\",\n                MouseAction.RightDoubleClick => \"RightDoubleClick\",\n                MouseAction.MiddleDoubleClick => \"MiddleDoubleClick\",\n                _ => throw new InvalidEnumArgumentException(nameof(value), (int)mouseAction, typeof(MouseAction))\n            };\n        }\n\n        /// <summary>\n        /// Helper function similar to <see cref=\"Enum.IsDefined{MouseAction}(MouseAction)\"/>, just lighter and faster.\n        /// </summary>\n        /// <param name=\"mouseAction\">The value to test against.</param>\n        /// <returns><see langword=\"true\"/> if <paramref name=\"mouseAction\"/> falls in enumeration range, <see langword=\"false\"/> otherwise.</returns>\n        internal static bool IsDefinedMouseAction(MouseAction mouseAction)\n        {\n            return mouseAction >= MouseAction.None && mouseAction <= MouseAction.MiddleDoubleClick;\n        }\n    }\n}\n","sourceCodeStart":82,"sourceCodeEnd":115,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs#L82-L115","documentation":"MouseActionConverter.ConvertTo accepts only the seven defined MouseAction values; anything else throws InvalidEnumArgumentException for 'value'. This converts the enum back to its string form for serialization/XAML output, so undefined enum values cannot be represented.","triggerScenarios":"Calling ConvertTo with a MouseAction cast from an undefined int (e.g. (MouseAction)42 or negative values).","commonSituations":"Serializing gesture settings that were loaded from untrusted/config data with out-of-range integers.","solutions":["Only pass defined MouseAction enum members (LeftClick..MiddleDoubleClick)","Validate the value against the known members before calling ConvertTo","Sanitize config data with the enum converter when loading"],"exampleFix":"// before\nstring s = (string)converter.ConvertTo(null, culture, (MouseAction)rawInt, typeof(string));\n// after\nvar action = (MouseAction)rawInt;\nbool defined = action is MouseAction.LeftClick or MouseAction.RightClick or MouseAction.MiddleClick\n    or MouseAction.WheelClick or MouseAction.LeftDoubleClick or MouseAction.RightDoubleClick or MouseAction.MiddleDoubleClick;\nif (defined)\n{\n    string s = (string)converter.ConvertTo(null, culture, action, typeof(string));\n}","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(MouseAction), mouseAction)) throw new ArgumentOutOfRangeException(nameof(mouseAction));","typeGuard":"static bool IsDefinedMouseAction(MouseAction a) => a is MouseAction.LeftClick or MouseAction.RightClick or MouseAction.MiddleClick or MouseAction.WheelClick or MouseAction.LeftDoubleClick or MouseAction.RightDoubleClick or MouseAction.MiddleDoubleClick;","tryCatchPattern":"try { s = (string)converter.ConvertTo(null, culture, mouseAction, typeof(string)); } catch (InvalidEnumArgumentException ex) { /* undefined MouseAction value */ }","preventionTips":["Never cast raw ints to MouseAction without Enum.IsDefined validation","Sanitize persisted gesture data when loading from config"],"tags":["wpf","input","mouseaction","converter","enum"],"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"}