{"record":{"id":"c73e429566a2b6b1","repo":"dotnet/wpf","slug":"flat-enums-can-t-be-flag-type-0","errorCode":null,"errorMessage":"Flat enums can't be flag type: {0}","messagePattern":"Flat enums can't be flag type: (.+?)","errorType":"exception","errorClass":"ApplicationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WpfGfx/codegen/mcg/helpers/EnumHelper.cs","lineNumber":78,"sourceCode":"                        [[/inline]]\n                        ;\n                }\n                else\n                {\n                    return comment +\n                        [[inline]]\n                            BEGIN_MILENUM( [[e.UnmanagedName]] )\n                                [[GetEnumMembers(e)]]\n                            END_MILENUM\n                        [[/inline]]\n                        ;\n                }\n            }\n            else\n            {\n                if (e.Flags)\n                {\n                    throw new ApplicationException(String.Format(\n                        \"Flat enums can't be flag type: {0}\",\n                        e.UnmanagedName));\n                }\n\n                return comment +\n                    [[inline]]\n                        typedef enum\n                        {\n                            [[GetEnumMembers(e)]]\n\n                            [[e.UnmanagedName]]_[[GetNativeEnumForceSize(e)]]\n                        } [[e.UnmanagedName]];\n                    [[/inline]]\n                    ;\n            }\n        }\n\n        /// <summary>","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WpfGfx/codegen/mcg/helpers/EnumHelper.cs#L60-L96","documentation":"FormatNativeEnum in the mcg codegen tool's EnumHelper throws this ApplicationException when the model contains a flat (non-nested) enum that is marked as a flags enum (e.Flags). The generator only supports flag enums in the nested form, so a flat enum declared with flags is a modeling error in the input XML and code generation aborts.","triggerScenarios":"Running the mcg generator over an XML definition where an enum is declared flat but flagged as a flags type (Flags=true), causing FormatNativeEnum to hit its invariant check while emitting native enum comments/values.","commonSituations":"Hand-editing the mcg input XML and adding a flags attribute to a plain enum; migrating enum definitions between generator versions with different flag-enum rules; copy-pasting an enum declaration from another model that used the nested flag form.","solutions":["Remove the flags marker from the flat enum in the input XML, or","Convert the enum to the nested form the generator supports for flag types.","Regenerate and confirm the model validates before re-running the generator."],"exampleFix":"// before (model XML)\n<enum name='MyEnum' flags='true'> ... flat values ... </enum>\n// after\n<enum name='MyEnum'> ... flat values ... </enum>","handlingStrategy":"validation","validationCode":"// before running the generator, check the model:\n// every enum with flags='true' must use the nested form\nforeach (var e in model.Enums)\n    if (e.Flags && e.IsFlat) throw new Exception($\"{e.UnmanagedName}: flat enums cannot be flags\");","typeGuard":null,"tryCatchPattern":"try { generator.Run(args); }\ncatch (ApplicationException ex) when (ex.Message.StartsWith(\"Flat enums can't be flag type\")) { /* fix the enum declaration in the model XML */ }","preventionTips":["Validate the model XML against the schema before generating.","Never hand-add a flags attribute to a flat enum declaration.","Keep enum definitions in the nested form when they need flag semantics."],"tags":["enum","flags","codegen","modeling-error"],"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"}