{"record":{"id":"fb55e8f065de6896","repo":"JamesNK/Newtonsoft.Json","slug":"enum-type-0-is-not-a-set-of-flags","errorCode":null,"errorMessage":"Enum type {0} is not a set of flags.","messagePattern":"Enum type (.+?) is not a set of flags\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Utilities/EnumUtils.cs","lineNumber":100,"sourceCode":"#endif\n\n                resolvedNames[i] = key.Value2 != null\n                    ? key.Value2.GetPropertyName(resolvedName, hasSpecifiedName)\n                    : resolvedName;\n            }\n\n            bool isFlags = enumType.IsDefined(typeof(FlagsAttribute), false);\n\n            return new EnumInfo(isFlags, values, names, resolvedNames);\n        }\n\n        public static IList<T> GetFlagsValues<T>(T value) where T : struct\n        {\n            Type enumType = typeof(T);\n\n            if (!enumType.IsDefined(typeof(FlagsAttribute), false))\n            {\n                throw new ArgumentException(\"Enum type {0} is not a set of flags.\".FormatWith(CultureInfo.InvariantCulture, enumType));\n            }\n\n            Type underlyingType = Enum.GetUnderlyingType(value.GetType());\n\n            ulong num = ToUInt64(value);\n            EnumInfo enumNameValues = GetEnumValuesAndNames(enumType);\n            IList<T> selectedFlagsValues = new List<T>();\n\n            for (int i = 0; i < enumNameValues.Values.Length; i++)\n            {\n                ulong v = enumNameValues.Values[i];\n\n                if ((num & v) == v && v != 0)\n                {\n                    selectedFlagsValues.Add((T)Convert.ChangeType(v, underlyingType, CultureInfo.CurrentCulture));\n                }\n            }\n","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Utilities/EnumUtils.cs#L82-L118","documentation":"GetFlagsValues<T> (EnumUtils.cs:94-125) decomposes a value into its constituent flag members. It requires the enum to carry [Flags]; it checks IsDefined(typeof(FlagsAttribute)) at EnumUtils.cs:98-101 and throws ArgumentException if the attribute is absent, because bitwise decomposition is meaningless for non-flags enums.","triggerScenarios":"Calling GetFlagsValues on an enum that lacks the [Flags] attribute (e.g. a plain sequential enum used as if it were a bit field).","commonSituations":"Treating a regular enum as a bitmask; refactoring that removed [Flags] by accident; using flags-style APIs (StringEnumConverter with AllowIntegerValues + flags) on non-flags enums.","solutions":["Add [Flags] to the enum definition.","If the type genuinely isn't a bit field, use a non-flags API (TryToString / direct cast) instead of GetFlagsValues.","Verify the enum design before relying on bitwise composition."],"exampleFix":"// before\nenum Perm { Read = 1, Write = 2 } // no [Flags] -> GetFlagsValues throws\n// after\n[Flags]\nenum Perm { Read = 1, Write = 2 }","handlingStrategy":"validation","validationCode":"// Verify [Flags] before calling a flags-style API.\nif (!enumType.IsDefined(typeof(FlagsAttribute), false))\n    throw new ArgumentException($\"{enumType} must have [Flags] to decompose.\");","typeGuard":"static bool IsFlagsEnum(Type t) => t.IsEnum && t.IsDefined(typeof(FlagsAttribute), false);","tryCatchPattern":"try { EnumUtils.GetFlagsValues(value); } catch (ArgumentException ex) when (ex.Message.Contains(\"not a set of flags\")) { /* add [Flags] or use a non-flags API */ }","preventionTips":["Add [Flags] to any enum used as a bitmask.","Use non-decomposing APIs for plain sequential enums.","Confirm the enum design before relying on bitwise composition."],"tags":["enum","newtonsoft-json","configuration"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}