dotnet/maui · error · InvalidOperationException

Cannot convert {self} to {nameof(EffectiveFlowDirection)}.

Error message

Cannot convert {self} to {nameof(EffectiveFlowDirection)}.

What it means

ToEffectiveFlowDirection switches over the FlowDirection enum (MatchParent, LeftToRight, RightToLeft) and maps each to an EffectiveFlowDirection. The default branch throws InvalidOperationException for any FlowDirection value not in that set — reachable only if the enum is cast from an invalid integer or a future/undefined member.

Source

Thrown at src/Controls/src/Core/EffectiveFlowDirectionExtensions.cs:38

						return EffectiveFlowDirection.Explicit;
					}
					else
					{
						return default(EffectiveFlowDirection);
					}

				case FlowDirection.RightToLeft:
					if (isExplicit)
					{
						return EffectiveFlowDirection.RightToLeft | EffectiveFlowDirection.Explicit;
					}
					else
					{
						return EffectiveFlowDirection.RightToLeft;
					}

				default:
					throw new InvalidOperationException($"Cannot convert {self} to {nameof(EffectiveFlowDirection)}.");
			}
		}

		internal static FlowDirection ToFlowDirection(this EffectiveFlowDirection self)
		{
			if (self.IsLeftToRight())
				return FlowDirection.LeftToRight;
			else
				return FlowDirection.RightToLeft;

			throw new InvalidOperationException($"Cannot convert {self} to {nameof(FlowDirection)}.");
		}

		/// <summary>Returns <see langword="true"/> if the flow direction is right-to-left. Otherwise, returns <see langword="false"/>.</summary>
		/// <param name="self">The enumeration value on which this method operates.</param>
		/// <returns><see langword="true"/> if the flow direction is right-to-left. Otherwise, <see langword="false"/>.</returns>
		public static bool IsRightToLeft(this EffectiveFlowDirection self)
		{

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Validate the FlowDirection value with Enum.IsDefined before converting.
  2. Ensure serialized/bound data only contains defined FlowDirection members.
  3. Default unknown values to FlowDirection.MatchParent instead of casting raw integers.

Example fix

// before
var efd = ((FlowDirection)99).ToEffectiveFlowDirection(); // default branch throws

// after
FlowDirection fd = (FlowDirection)99;
if (!Enum.IsDefined(typeof(FlowDirection), fd)) fd = FlowDirection.MatchParent;
var efd = fd.ToEffectiveFlowDirection();
Defensive patterns

Strategy: validation

Validate before calling

static bool IsValidFlowDirection(FlowDirection fd) => Enum.IsDefined(typeof(FlowDirection), fd);
// usage:
if (!IsValidFlowDirection(fd)) fd = FlowDirection.MatchParent;

Type guard

static bool IsDefinedFlowDirection(FlowDirection fd) => Enum.IsDefined(typeof(FlowDirection), fd);

Prevention

When it happens

Trigger: Casting an arbitrary/invalid integer to FlowDirection (e.g. (FlowDirection)99) and converting it; reflection or deserialization producing an out-of-range enum value.

Common situations: Deserialization binding a numeric value to a FlowDirection property; (FlowDirection) casts from untrusted input; enum defined with [Flags] misuse.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/1ef5baeed23a1dc2. Report an issue: GitHub.