{"record":{"id":"32b6eef3c5ba327d","repo":"AvaloniaUI/Avalonia","slug":"styles-can-only-be-applied-to-avaloniaobjects","errorCode":null,"errorMessage":"Styles can only be applied to AvaloniaObjects.","messagePattern":"Styles can only be applied to AvaloniaObjects\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Styling/StyleBase.cs","lineNumber":117,"sourceCode":"                for (var i = 0; i < _children.Count; ++i)\n                {\n                    if (_children[i].TryGetResource(key, themeVariant, out result))\n                        return true;\n                }\n            }\n\n            result= null;\n            return false;\n        }\n\n        internal ValueFrame Attach(\n            StyledElement target,\n            IStyleActivator? activator,\n            FrameType type,\n            bool canShareInstance)\n        {\n            if (target is not AvaloniaObject ao)\n                throw new InvalidOperationException(\"Styles can only be applied to AvaloniaObjects.\");\n\n            StyleInstance instance;\n\n            if (_sharedInstance is not null && canShareInstance)\n            {\n                instance = _sharedInstance;\n            }\n            else\n            {\n                canShareInstance &= activator is null;\n\n                instance = new StyleInstance(this, activator, type);\n\n                if (_setters is not null)\n                {\n                    foreach (var setter in _setters)\n                    {\n                        var setterInstance = setter.Instance(instance, target);","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Styling/StyleBase.cs#L99-L135","documentation":"Thrown by StyleBase.Attach when applying a StyleInstance to a target element. Attach receives a StyledElement and asserts it is an AvaloniaObject. Because StyledElement derives from Animatable which derives from AvaloniaObject, the C# type system already guarantees this for any non-null instance, so the runtime check exists as a defensive invariant: it fires only when target is null or when Attach is reached through non-type-safe paths (reflection, IL weaving, corrupted runtime state).","triggerScenarios":"Attach is internal and invoked by the styling system during selector matching. The throw is reached when (a) a null StyledElement is passed to Attach, or (b) Attach is invoked via reflection/dynamic dispatch with an argument whose runtime type is not AvaloniaObject. Neither path is reachable through the public Styles/Control.Styles API under normal compilation.","commonSituations":"Reflection-based or code-generation styling frameworks that bypass the public API; AOT/trimmer scenarios that strip AvaloniaObject base metadata; a null control reference surviving into the style-application phase (e.g. a selector matching an element that has since been detached and nulled); custom IStyleInstance implementations that fabricate an Attach call.","solutions":["If you are calling Attach via reflection, stop: use the public styling surface (Styles.Add on a control's or Application's Styles collection).","If the error appears during normal styling, capture a stack trace and confirm the StyledElement being styled is non-null at match time; a null target here usually means a control was detached/collected while a style frame was still being attached.","Treat this as a framework-bug signal if reached through public API and file an issue with the repro."],"exampleFix":"// before\nstyle.Attach(nullElement, activator, type, canShareInstance); // nullElement is null\n\n// after\nif (target is AvaloniaObject) style.Attach(target, activator, type, canShareInstance);","handlingStrategy":"validation","validationCode":"// Guard before invoking Attach-equivalent logic: confirm non-null and AvaloniaObject-derived.\nif (target is null || target is not AvaloniaObject)\n{\n    // skip attaching; log target identity for diagnosis\n    return;\n}","typeGuard":"// C# narrowing: StyledElement already guarantees AvaloniaObject, so guard mainly against null.\nstatic bool IsValidStyleTarget(StyledElement? target) => target is AvaloniaObject;","tryCatchPattern":null,"preventionTips":["Do not invoke the internal Attach API via reflection; use the public Styles.Add path.","Null-check any control reference before letting it participate in style application.","If authoring a custom IStyleInstance, never call Attach with a detached/collected element."],"tags":["avalonia","styling","invariant","type-system","null-target"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}