{"record":{"id":"f831272d6ee510a8","repo":"dotnet/wpf","slug":"sr-format-sr-freezable-cantbefrozen-gettype-fullname","errorCode":null,"errorMessage":"SR.Format(SR.Freezable_CantBeFrozen, GetType().FullName)","messagePattern":"SR\\.Format\\(SR\\.Freezable_CantBeFrozen, GetType\\(\\)\\.FullName\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Freezable.cs","lineNumber":616,"sourceCode":"        /// This ensures that the object is being accessed from a valid thread.\n        /// </summary>\n        protected void ReadPreamble()\n        {\n            VerifyAccess();\n        }\n\n        /// <summary>\n        /// Extenders of Freezable must call this method prior to changing the state\n        /// of the object (e.g. the beginning of a property setter.)  This ensures that\n        /// the object is not frozen and is being accessed from a valid thread.\n        /// </summary>\n        protected void WritePreamble()\n        {\n            VerifyAccess();\n\n            if (IsFrozenInternal)\n            {\n                throw new InvalidOperationException(\n                    SR.Format(SR.Freezable_CantBeFrozen,GetType().FullName));\n            }\n        }\n\n        /// <summary>\n        /// Extenders of Freezable must call this method at the end of an API which\n        /// changed the state of the object (e.g., at the end of a property setter) to\n        /// raise the Changed event.  Multiple state changes within a method or\n        /// property may be \"batched\" into a single call to WritePostscript().\n        /// </summary>\n        protected void WritePostscript()\n        {\n            FireChanged();\n        }\n\n        /// <summary>\n        /// Extenders of Freezable call this to set in a new value for internal\n        /// properties or other embedded values that themselves are DependencyObjects.","sourceCodeStart":598,"sourceCodeEnd":634,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Freezable.cs#L598-L634","documentation":"Freezable.WritePreamble is called at the start of every mutating API to enforce thread affinity and immutability. If the Freezable is already frozen (IsFrozenInternal), any mutation attempt throws InvalidOperationException with Freezable_CantBeFrozen naming the concrete type — frozen objects must never be modified.","triggerScenarios":"Calling any setter or mutating method (Color, Geometry data, collection Add/Remove, etc.) on a Freezable that has been frozen via Freeze(), GetAsFrozen, or implicit XAML freezing.","commonSituations":"Keeping a cached frozen resource (e.g. Brushes.Red or a frozen Brush from a shared ResourceDictionary) and trying to change its Color; mutating a template/animation object after it was frozen for cross-thread use; deserializing into a frozen instance.","solutions":["Clone the Freezable with Clone()/CloneCurrentValue() and mutate the unfrozen copy","Keep an unfrozen instance if mutation is expected later","Check IsFrozen before mutating and branch to the clone path","Share frozen instances only as read-only resources"],"exampleFix":"// before\nsharedBrush.Color = Colors.Blue; // frozen -> throws\n// after\nvar copy = sharedBrush.Clone();\ncopy.Color = Colors.Blue;\ncopy.Freeze();","handlingStrategy":"type-guard","validationCode":"if (obj.IsFrozen) { obj = obj.Clone(); } obj.Color = Colors.Blue; // mutate copy","typeGuard":"static bool IsMutable(System.Windows.Freezable f) => !f.IsFrozen;","tryCatchPattern":"try { obj.Color = Colors.Blue; }\ncatch (InvalidOperationException ex) when (obj.IsFrozen) { var copy = obj.Clone(); copy.Color = Colors.Blue; }","preventionTips":["Treat frozen Freezables as strictly read-only","Clone before mutating shared/cached resources","Check IsFrozen in code paths that receive Freezables from dictionaries or templates"],"tags":["wpf","freezable","immutable","threading"],"backgroundTag":"frozen-object-modification","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}