AvaloniaUI/Avalonia · error · ExpressionParseException

Dynamic expressions are not allowed in binding expressions.

Error message

Dynamic expressions are not allowed in binding expressions.

What it means

Thrown unconditionally by BindingExpressionVisitor.VisitDynamic when a DynamicExpression is encountered in the compiled binding tree. Dynamic expressions arise from C# dynamic dispatch (using the dynamic keyword) and are not supported because the binding system requires statically known type information to build property accessors.

Source

Thrown at src/Avalonia.Base/Data/Core/Parsers/BindingExpressionVisitor.cs:215

    protected override Expression VisitBlock(BlockExpression node)
    {
        throw new ExpressionParseException(0, $"Invalid expression type in binding expression: {node.NodeType}.");
    }

    protected override CatchBlock VisitCatchBlock(CatchBlock node)
    {
        throw new ExpressionParseException(0, "Catch blocks are not allowed in binding expressions.");
    }

    protected override Expression VisitConditional(ConditionalExpression node)
    {
        throw new ExpressionParseException(0, $"Invalid expression type in binding expression: {node.NodeType}.");
    }

    protected override Expression VisitDynamic(DynamicExpression node)
    {
        throw new ExpressionParseException(0, "Dynamic expressions are not allowed in binding expressions.");
    }

    protected override ElementInit VisitElementInit(ElementInit node)
    {
        throw new ExpressionParseException(0, "Element init expressions are not valid in a binding expression.");
    }

    protected override Expression VisitGoto(GotoExpression node)
    {
        throw new ExpressionParseException(0, "Goto expressions are not supported in binding expressions.");
    }

    protected override Expression VisitInvocation(InvocationExpression node)
    {
        throw new ExpressionParseException(0, $"Invalid expression type in binding expression: {node.NodeType}.");
    }

    protected override Expression VisitLabel(LabelExpression node)

View on GitHub (pinned to 11c5427268)

Solutions

  1. Replace dynamic with a concrete type in the model and view model so the expression tree contains statically typed member access.
  2. If binding to dynamic data, create a strongly typed wrapper or DTO that the binding lambda can reference.
  3. Use a custom IPropertyAccessor plugin if you genuinely need dynamic property resolution at the data layer.

Example fix

// before: dynamic-typed property
public class MyModel {
    public dynamic Data { get; set; }  // causes DynamicExpression
}
// binding: x => x.Data.Name

// after: concrete type
public class MyModel {
    public MyData Data { get; set; }  // statically typed
}
Defensive patterns

Strategy: type-guard

Validate before calling

// Ensure no dynamic-typed members appear in the binding path
static bool HasNoDynamicTypes<TIn, TOut>(Expression<Func<TIn, TOut>> expr)
{
    bool found = false;
    expr.Body.Visit(b =>
    {
        if (b.Type == typeof(object) && b is DynamicExpression) found = true;
    });
    return !found;
}

Type guard

static bool IsStaticallyTyped(Expression expr)
{
    return expr.Type != typeof(dynamic) && !(expr is DynamicExpression);
}

Try / catch

try
{
    var path = BindingExpressionVisitor<TViewModel>.BuildPath<TProp>(expr);
}
catch (ExpressionParseException ex) when (ex.Message.Contains("Dynamic expressions"))
{
    logger.LogError("Dynamic types are not supported in compiled bindings. Use a concrete type.");
}

Prevention

When it happens

Trigger: Using a dynamic-typed variable or parameter in a compiled binding lambda where the C# compiler generates dynamic call sites. This can occur when the binding source or an intermediate property is typed as 'dynamic'.

Common situations: Binding to a view model or model whose properties are typed as dynamic (e.g., deserialized JSON with ExpandoObject); using dynamic to avoid strongly typed models; interop with COM or DLR-based objects in the binding path.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/d035b25eefa6b7d3. Report an issue: GitHub.