dotnet/wpf · error · ApplicationException
Flat enums can't be flag type
Error message
Flat enums can't be flag type: {0} What it means
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.
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.
Example fix
// before (model XML) <enum name='MyEnum' flags='true'> ... flat values ... </enum> // after <enum name='MyEnum'> ... flat values ... </enum>
Defensive patterns
Strategy: validation
Validate before calling
// before running the generator, check the model:
// every enum with flags='true' must use the nested form
foreach (var e in model.Enums)
if (e.Flags && e.IsFlat) throw new Exception($"{e.UnmanagedName}: flat enums cannot be flags"); Try / catch
try { generator.Run(args); }
catch (ApplicationException ex) when (ex.Message.StartsWith("Flat enums can't be flag type")) { /* fix the enum declaration in the model XML */ } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- InvalidEnumArgumentException("options", (int)options…
- Animation_UnrecognizedHandoffBehavior
- args
- ArgumentOutOfRangeException(authentication)
- ArgumentOutOfRangeException(authenticationType)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/c73e429566a2b6b1.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WpfGfx/codegen/mcg/helpers/EnumHelper.cs:78
[[/inline]]
;
}
else
{
return comment +
[[inline]]
BEGIN_MILENUM( [[e.UnmanagedName]] )
[[GetEnumMembers(e)]]
END_MILENUM
[[/inline]]
;
}
}
else
{
if (e.Flags)
{
throw new ApplicationException(String.Format(
"Flat enums can't be flag type: {0}",
e.UnmanagedName));
}
return comment +
[[inline]]
typedef enum
{
[[GetEnumMembers(e)]]
[[e.UnmanagedName]]_[[GetNativeEnumForceSize(e)]]
} [[e.UnmanagedName]];
[[/inline]]
;
}
}
/// <summary>View on GitHub (pinned to 81131a70a4)