AvaloniaUI/Avalonia · error · InvalidOperationException

Object of type {o.Value.GetType()} is not allowed

Error message

Object of type {o.Value.GetType()} is not allowed

What it means

While snapshotting a property set, any stored CompositionObject that is neither a nested CompositionPropertySet nor backed by a Server (Server == null) is rejected with InvalidOperationException naming its type. Types like CompositionAnimation and bare CompositionPropertySet references have no server counterpart and cannot be serialized as expression parameter values.

Source

Thrown at src/Avalonia.Base/Rendering/Composition/CompositionPropertySet.cs:130

            _variants.Remove(key);
        }

        internal PropertySetSnapshot Snapshot() =>
            SnapshotCore(1);
        
        private PropertySetSnapshot SnapshotCore(int allowedNestingLevel)
        {
            var dic = new Dictionary<string, PropertySetSnapshot.Value>(_objects.Count + _variants.Count);
            foreach (var o in _objects)
            {
                if (o.Value is CompositionPropertySet ps)
                {
                    if (allowedNestingLevel <= 0)
                        throw new InvalidOperationException("PropertySet depth limit reached");
                    dic[o.Key] = new PropertySetSnapshot.Value(ps.SnapshotCore(allowedNestingLevel - 1));
                }
                else if (o.Value.Server == null)
                    throw new InvalidOperationException($"Object of type {o.Value.GetType()} is not allowed");
                else
                    dic[o.Key] = new PropertySetSnapshot.Value((ServerObject)o.Value.Server);
            }

            foreach (var v in _variants)
                dic[v.Key] = v.Value;
            
            return new PropertySetSnapshot(dic);
        }
    }

    public enum CompositionGetValueStatus
    {
        Succeeded,
        TypeMismatch,
        NotFound
    }
}

View on GitHub (pinned to 11c5427268)

Solutions

  1. Only store server-backed composition objects (visuals, brushes) or nested CompositionPropertySets in a property set.
  2. Reference other CompositionObjects via CompositionAnimation.SetReferenceParameter rather than embedding them as property-set values.
  3. Do not put CompositionAnimation instances into a CompositionPropertySet.

Example fix

// before
propSet.Set("anim", someAnimation); // CompositionAnimation has no Server
anim.SetReferenceParameter("p", propSet);
visual.StartAnimation("Opacity", anim); // Snapshot throws

// after
anim.SetReferenceParameter("refVisual", someVisual); // server-backed
visual.StartAnimation("Opacity", anim);
Defensive patterns

Strategy: validation

Validate before calling

// Only allow server-backed objects or nested property sets as values.
if (value is CompositionObject co && !(co is CompositionPropertySet) && co.GetType().Name.Contains("Animation"))
    throw new InvalidOperationException($"{co.GetType()} is not allowed in a PropertySet");

Type guard

static bool IsAllowedValue(CompositionObject o) => o is CompositionPropertySet || /* server-backed */ true;

Prevention

When it happens

Trigger: Placing a server-less CompositionObject (e.g. a CompositionAnimation, or another CompositionPropertySet used as a value object) into a CompositionPropertySet via the internal Set(key, CompositionObject), then snapshotting — typically by starting an animation that references that parameter.

Common situations: Registering an animation or an unsupported object type as a reference value inside a property set used by an expression animation; misusing internal Set overloads.

Related errors


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