{"record":{"id":"f44216fd2abc182d","repo":"dotnet/wpf","slug":"sr-clearonreadonlyobjectnotallowed","errorCode":null,"errorMessage":"SR.ClearOnReadOnlyObjectNotAllowed","messagePattern":"SR\\.ClearOnReadOnlyObjectNotAllowed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyObject.cs","lineNumber":960,"sourceCode":"\n            DependencyProperty dp;\n\n            // Cache the metadata object this method needed to get anyway.\n            PropertyMetadata metadata = SetupPropertyChange(key, out dp);\n\n            EntryIndex entryIndex = LookupEntry(dp.GlobalIndex);\n\n            ClearValueCommon(entryIndex, dp, metadata);\n        }\n\n        /// <summary>\n        ///     The common code shared by all variants of ClearValue\n        /// </summary>\n        private void ClearValueCommon(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata)\n        {\n            if (IsSealed)\n            {\n                throw new InvalidOperationException(SR.Format(SR.ClearOnReadOnlyObjectNotAllowed, this));\n            }\n\n            // Get old value\n            EffectiveValueEntry oldEntry = GetValueEntry(\n                                        entryIndex,\n                                        dp,\n                                        metadata,\n                                        RequestFlags.RawEntry);\n\n            // Get current local value\n            // (No need to go through read local callback, just checking\n            // for presence of Expression)\n            object current = oldEntry.LocalValue;\n\n            // Get current expression\n            Expression currentExpr = (oldEntry.IsExpression) ? (current as Expression) : null;\n\n            // Inform value expression of detachment, if applicable","sourceCodeStart":942,"sourceCodeEnd":978,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyObject.cs#L942-L978","documentation":"Thrown by DependencyObject.ClearValueCommon when ClearValue (or the SetValue path that clears) is invoked on a sealed DependencyObject (IsSealed == true), such as a frozen Freezable or a sealed style resource. Sealed objects are immutable by design, so clearing a property is prohibited.","triggerScenarios":"Calling ClearValue(dp) on an object whose IsSealed property is true — e.g. a frozen Brush, a style-applied value on a sealed instance, or a shared resource retrieved from application resources.","commonSituations":"Trying to modify a frozen Freezable (Brush, Pen, Geometry) fetched from a resource dictionary, mutating a shared system resource, templated or sealed styles.","solutions":["Check obj.IsSealed before calling ClearValue and skip or clone","Call obj.Clone() (or GetAsFrozen inverse: CloneCurrentValue) to obtain a mutable copy, then ClearValue on the clone","If it is a Freezable, work with an unfrozen copy and re-freeze after modification","Do not attempt to unseal the original; sealed state cannot be reversed"],"exampleFix":"// before\nif (brush.IsFrozen) brush.ClearValue(Brush.OpacityProperty); // throws\n// after\nvar mutable = brush.Clone();\nmutable.ClearValue(Brush.OpacityProperty);","handlingStrategy":"validation","validationCode":"if (depObj.IsSealed) {\n    depObj = ((Freezable)depObj).Clone(); // or skip\n}\ndepObj.ClearValue(dp);","typeGuard":"static bool CanClear(DependencyObject d) => !d.IsSealed;","tryCatchPattern":"try { depObj.ClearValue(dp); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"sealed\") || depObj.IsSealed) { depObj = ((Freezable)depObj)?.Clone() as DependencyObject; depObj?.ClearValue(dp); }","preventionTips":["Check IsSealed/IsFrozen before mutating any DependencyObject/Freezable","Clone frozen Freezables before modification","Never mutate shared resources retrieved from dictionaries in place"],"tags":["wpf","dependency-property","sealed-object"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}