{"record":{"id":"4880f3438b054c46","repo":"dotnet/maui","slug":"enumpicker-enumtype-property-must-be-enumeration","errorCode":null,"errorMessage":"EnumPicker: EnumType property must be enumeration type","messagePattern":"EnumPicker: EnumType property must be enumeration type","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Controls/samples/Controls.Sample/Controls/EnumPicker.cs","lineNumber":22,"sourceCode":"\nnamespace Maui.Controls.Sample.Controls\n{\n\tclass EnumPicker : Picker\n\t{\n\t\tpublic static readonly BindableProperty EnumTypeProperty =\n\t\t\tBindableProperty.Create(nameof(EnumType), typeof(Type), typeof(EnumPicker),\n\t\t\t\tpropertyChanged: (bindable, oldValue, newValue) =>\n\t\t\t\t{\n\t\t\t\t\tEnumPicker picker = (EnumPicker)bindable;\n\n\t\t\t\t\tif (oldValue != null)\n\t\t\t\t\t{\n\t\t\t\t\t\tpicker.ItemsSource = null;\n\t\t\t\t\t}\n\t\t\t\t\tif (newValue != null)\n\t\t\t\t\t{\n\t\t\t\t\t\tif (!((Type)newValue).GetTypeInfo().IsEnum)\n\t\t\t\t\t\t\tthrow new ArgumentException(\"EnumPicker: EnumType property must be enumeration type\");\n\n\t\t\t\t\t\tpicker.ItemsSource = Enum.GetValues((Type)newValue);\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\tpublic Type EnumType\n\t\t{\n\t\t\tset => SetValue(EnumTypeProperty, value);\n\t\t\tget => (Type)GetValue(EnumTypeProperty);\n\t\t}\n\t}\n}","sourceCodeStart":4,"sourceCodeEnd":34,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/samples/Controls.Sample/Controls/EnumPicker.cs#L4-L34","documentation":"This sample EnumPicker (a Picker subclass) exposes an EnumType bindable property. When EnumType is set, the propertyChanged callback checks whether the assigned Type is an enum via IsEnum. If it is not (e.g., a class, struct, or interface), it throws ArgumentException because Enum.GetValues would fail or produce nonsensical results for a non-enum type.","triggerScenarios":"Binding or assigning EnumType to a Type that is not an enumeration — e.g., `enumPicker.EnumType = typeof(string)` or `enumPicker.EnumType = typeof(MyClass)`. Setting EnumType from a data source that resolves to a non-enum Type.","commonSituations":"Binding EnumType to a property whose type is not constrained at compile time. Passing typeof(T) where T is not an enum generic constraint. Mistakenly assigning the value type rather than the enum type. Dynamic type resolution from reflection producing a non-enum Type.","solutions":["Only assign enumeration types to EnumType: `enumPicker.EnumType = typeof(MyEnum);`","Add a runtime guard before assignment: `if (typeof(T).GetTypeInfo().IsEnum) enumPicker.EnumType = typeof(T);`","Add a generic constraint `where T : struct, Enum` if EnumType is set from a generic method."],"exampleFix":"// before\nenumPicker.EnumType = typeof(MySettingsClass); // not an enum\n\n// after\nenumPicker.EnumType = typeof(MySettingsEnum); // actual enum","handlingStrategy":"validation","validationCode":"var candidateType = typeof(MyEnum);\nif (candidateType.GetTypeInfo().IsEnum)\n    enumPicker.EnumType = candidateType;\nelse\n    throw new InvalidOperationException($\"{candidateType.Name} is not an enum.\");","typeGuard":"static bool IsEnumType(Type? type) => type?.GetTypeInfo().IsEnum ?? false;","tryCatchPattern":null,"preventionTips":["Only assign typeof() of actual enum types to EnumType.","Add a generic constraint `where T : struct, Enum` when setting EnumType from generics.","Validate the type with IsEnum before assignment in dynamic scenarios."],"tags":["maui","controls","picker","enum","argument","validation","sample"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}