dotnet/maui · error · ArgumentException

Unhandled expression type: '{0}'

Error message

Unhandled expression type: '{0}'

What it means

Thrown by WPFExpressionSearch.Visit when the expression-tree visitor encounters an ExpressionType not covered by its switch statement (it handles Lambda, MemberAccess, Call, New, arrays, MemberInit, ListInit, Conditional, Invoke, Constant). Any other node type — e.g. Coalesce, TypeIs, Convert with custom logic, Quote — falls through to the default throw.

Source

Thrown at src/Compatibility/Core/src/WPF/WPFExpressionSearch.cs:121

				case ExpressionType.Invoke:
					var invocation = (InvocationExpression)expression;
					VisitList(invocation.Arguments, Visit);
					Visit(invocation.Expression);
					break;
				case ExpressionType.MemberInit:
					var init = (MemberInitExpression)expression;
					VisitList(init.NewExpression.Arguments, Visit);
					VisitList(init.Bindings, VisitBinding);
					break;
				case ExpressionType.ListInit:
					var init1 = (ListInitExpression)expression;
					VisitList(init1.NewExpression.Arguments, Visit);
					VisitList(init1.Initializers, initializer => VisitList(initializer.Arguments, Visit));
					break;
				case ExpressionType.Constant:
					break;
				default:
					throw new ArgumentException(string.Format("Unhandled expression type: '{0}'", expression.NodeType));
			}
		}

		void VisitBinding(MemberBinding binding)
		{
			switch (binding.BindingType)
			{
				case MemberBindingType.Assignment:
					Visit(((MemberAssignment)binding).Expression);
					break;
				case MemberBindingType.MemberBinding:
					VisitList(((MemberMemberBinding)binding).Bindings, VisitBinding);
					break;
				case MemberBindingType.ListBinding:
					VisitList(((MemberListBinding)binding).Initializers, initializer => VisitList(initializer.Arguments, Visit));
					break;
				default:
					throw new ArgumentException(string.Format("Unhandled binding type '{0}'", binding.BindingType));

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Simplify the expression to use only supported node types (member access, method calls, lambdas, conditionals).
  2. Avoid null-coalescing (??) and type-test (is) operators in expressions passed to the visitor.
  3. If a specific ExpressionType is needed, extend WPFExpressionSearch.Visit with an additional case.

Example fix

// before
Expression<Func<ViewModel, string>> expr = vm => vm.Name ?? "default";
var search = new WPFExpressionSearch();
var names = search.FindMembers(expr);

// after — avoid ?? which produces Coalesce (unsupported)
Expression<Func<ViewModel, string>> expr = vm => vm.Name;
var names = search.FindMembers(expr);
Defensive patterns

Strategy: validation

Validate before calling

static readonly HashSet<ExpressionType> Supported = new()
{
    ExpressionType.Lambda, ExpressionType.MemberAccess, ExpressionType.Call,
    ExpressionType.New, ExpressionType.NewArrayInit, ExpressionType.NewArrayBounds,
    ExpressionType.Invoke, ExpressionType.MemberInit, ExpressionType.ListInit,
    ExpressionType.Conditional, ExpressionType.Constant
};
bool IsSupported(Expression expr) => Supported.Contains(expr.NodeType);

Prevention

When it happens

Trigger: Passing a LINQ expression (e.g., for data binding or FindByName) to WPFExpressionSearch that contains an unsupported ExpressionType. For example, a null-coalescing operator (??) produces ExpressionType.Coalesce, which is not handled.

Common situations: Using complex LINQ expressions in binding helpers or FindByName calls on WPF; expressions using operators like ??, is, or complex conditional constructs that the visitor was not written to traverse.

Related errors


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