AvaloniaUI/Avalonia · error · ArgumentException
Need more than one selector to OR.
Error message
Need more than one selector to OR.
What it means
Thrown by the OrSelector constructor when the supplied selector list has zero or one elements. The `Selectors.Or(...)` helper requires at least two selectors; OR-ing a single or empty set is meaningless and treated as an error. Selectors are stored right-to-left, so the OR group needs multiple branches.
Source
Thrown at src/Avalonia.Base/Styling/OrSelector.cs:32
{
private readonly IReadOnlyList<Selector> _selectors;
private string? _selectorString;
private Type? _targetType;
/// <summary>
/// Initializes a new instance of the <see cref="OrSelector"/> class.
/// </summary>
/// <param name="selectors">The selectors to OR.</param>
public OrSelector(IReadOnlyList<Selector> selectors)
{
if (selectors is null)
{
throw new ArgumentNullException(nameof(selectors));
}
if (selectors.Count <= 1)
{
throw new ArgumentException("Need more than one selector to OR.");
}
_selectors = selectors;
}
/// <inheritdoc/>
internal override bool InTemplate => false;
/// <inheritdoc/>
internal override bool IsCombinator => false;
/// <inheritdoc/>
internal override Type? TargetType => _targetType ??= EvaluateTargetType();
/// <inheritdoc/>
public override string ToString(Style? owner) => ToString(owner, false);
/// <inheritdoc/>View on GitHub (pinned to 11c5427268)
Solutions
- Pass at least two selectors to Selectors.Or().
- If building dynamically, use the single selector directly when only one remains, and handle the empty case separately.
- Validate the array/list length before calling Or().
Example fix
// before
var s = Selectors.Or(items.Select(BuildSelector).ToArray()); // may be <=1
// after
var built = items.Select(BuildSelector).ToArray();
Selector result = built.Length switch
{
0 => throw new InvalidOperationException("no selectors"),
1 => built[0],
_ => Selectors.Or(built)
}; Defensive patterns
Strategy: validation
Validate before calling
if (selectors is null || selectors.Length < 2)
throw new ArgumentException("Or requires at least two selectors.", nameof(selectors));
var sel = Selectors.Or(selectors); Type guard
static bool CanOr(IReadOnlyList<Selector> s) => s is { Count: >= 2 }; Prevention
- Guard dynamic selector lists: use the single element directly when length is 1.
- Validate length before calling Selectors.Or().
- Write a small builder helper that enforces the minimum count.
When it happens
Trigger: Calling `Selectors.Or(singleSelector)` or `Selectors.Or()` with fewer than two arguments; passing a filtered/empty `IReadOnlyList<Selector>` to the `Or(IReadOnlyList<Selector>)` overload.
Common situations: Dynamically building an OR selector list that filtering reduced to one element; mistakenly wrapping a single selector in Or(); copy-paste leaving only one branch.
Related errors
- Need more than one query to OR.
- ControlTemplate styles cannot contain multiple template sele
- Child styles must have a nesting selector.
- Name may not be empty
- Setter.Property must be set.
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/d98fee5c6c98550d.
Report an issue: GitHub.