{"record":{"id":"640a09c0db6a5e6b","repo":"AvaloniaUI/Avalonia","slug":"resource-gettype-fullname-can-not-be-used-with","errorCode":null,"errorMessage":"{resource.GetType().FullName} can not be used with this DrawingContext","messagePattern":"(.+?) can not be used with this DrawingContext","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Rendering/Composition/Drawing/RenderDataDrawingContext.cs","lineNumber":63,"sourceCode":"        if (resource == null\n            || resource is IImmutableBrush\n            || resource is ImmutablePen\n            || resource is ImmutableTransform\n            || resource is CompositionBrush)\n            return;\n\n        if (resource is ICompositionRenderResource renderResource)\n        {\n            _resourcesHashSet ??= s_hashSetPool.Get();\n            if (!_resourcesHashSet.Add(renderResource))\n                return;\n\n            renderResource.AddRefOnCompositor(_compositor);\n            RenderData.AddResource(renderResource);\n            return;\n        }\n\n        throw new InvalidOperationException(resource.GetType().FullName + \" can not be used with this DrawingContext\");\n    }\n\n    private void PushedScope(int positionBefore) =>\n        (_pushStack ??= s_pushStackPool.Get()).Push(new PushEntry\n        {\n            Emitted = true,\n            PositionBefore = positionBefore,\n            PositionAfter = Stream.OpcodeLength,\n            DepthBefore = Stream.Depth - 1\n        });\n\n    private void PushedNoOpScope() =>\n        (_pushStack ??= s_pushStackPool.Get()).Push(new PushEntry { Emitted = false });\n\n    private void PopCore()\n    {\n        var entry = _pushStack!.Pop();\n        if (!entry.Emitted)","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Rendering/Composition/Drawing/RenderDataDrawingContext.cs#L45-L81","documentation":"RenderDataDrawingContext.AddResource accepts only a closed set of resource types: null, immutable brushes/pens/transforms, CompositionBrush, or any ICompositionRenderResource. Anything else throws InvalidOperationException stating the type 'can not be used with this DrawingContext'. The context records resources into a composition render-data stream, so it must be able to map each resource to a server object.","triggerScenarios":"Drawing into a composition-backed DrawingContext with a mutable/non-immutable IBrush, IPen, IGeometry, or other resource that is not an ICompositionRenderResource and not an immutable variant.","commonSituations":"Passing a custom mutable brush/pen implementation into a composition drawing context; using a Geometry subclass that is neither immutable nor a composition resource.","solutions":["Pass immutable variants (ImmutablePen, IImmutableBrush) or composition-backed resources (ICompositionRenderResource) to the drawing context.","Convert custom brushes/pens to immutable or composition-backed equivalents before drawing.","If you author custom resources, implement ICompositionRenderResource so AddResource can register them."],"exampleFix":"// before\ndc.DrawRectangle(myMutableBrush, myCustomPen, rect); // neither immutable nor composition -> throws\n\n// after\nvar brush = new ImmutableSolidColorBrush(myMutableBrush.Color, myMutableBrush.Opacity);\nvar pen = new ImmutablePen(brush, myCustomPen.Thickness);\ndc.DrawRectangle(brush, pen, rect);","handlingStrategy":"type-guard","validationCode":"static bool IsAcceptedByDrawingContext(object? r) =>\n    r is null or IImmutableBrush or ImmutablePen or ImmutableTransform or CompositionBrush or ICompositionRenderResource;","typeGuard":"static bool IsCompositionDrawableResource(object? r) =>\n    r is ICompositionRenderResource or IImmutableBrush or ImmutablePen or ImmutableTransform or CompositionBrush;","tryCatchPattern":null,"preventionTips":["Pass immutable or composition-backed resources to the composition drawing context.","Convert custom mutable brushes/pens to immutable before drawing.","Implement ICompositionRenderResource on custom resource types that must render in composition."],"tags":["composition","drawing","drawing-context","resource","type-mismatch"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}