{"record":{"id":"3e59ccd824156dd9","repo":"AvaloniaUI/Avalonia","slug":"the-metadata-is-read-only","errorCode":null,"errorMessage":"The metadata is read-only.","messagePattern":"The metadata is read-only\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/AvaloniaPropertyMetadata.cs","lineNumber":65,"sourceCode":"        public bool? EnableDataValidation { get; private set; }\n\n        /// <summary>\n        /// Gets whether this instance is read-only and can't be modified.\n        /// </summary>\n        public bool IsReadOnly { get; private set; }\n\n        /// <summary>\n        /// Merges the metadata with the base metadata.\n        /// </summary>\n        /// <param name=\"baseMetadata\">The base metadata to merge.</param>\n        /// <param name=\"property\">The property to which the metadata is being applied.</param>\n        public virtual void Merge(\n            AvaloniaPropertyMetadata baseMetadata, \n            AvaloniaProperty property)\n        {\n            if (IsReadOnly)\n            {\n                throw new InvalidOperationException(\"The metadata is read-only.\");\n            }\n\n            if (_defaultBindingMode == BindingMode.Default)\n            {\n                _defaultBindingMode = baseMetadata.DefaultBindingMode;\n            }\n\n            EnableDataValidation ??= baseMetadata.EnableDataValidation;\n        }\n\n        /// <summary>\n        /// Makes this instance read-only.\n        /// No further modifications are allowed after this call.\n        /// </summary>\n        public void Freeze()\n            => IsReadOnly = true;\n\n        /// <summary>","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/AvaloniaPropertyMetadata.cs#L47-L83","documentation":"Thrown by AvaloniaPropertyMetadata.Merge when the metadata instance has been frozen (IsReadOnly == true). Avalonia freezes property metadata once the property is fully registered so that shared metadata cannot be mutated after the fact. Calling Merge on already-frozen metadata violates that immutability contract.","triggerScenarios":"Calling metadata.Merge(baseMetadata, property) on an AvaloniaPropertyMetadata whose Freeze() method has already run (setting IsReadOnly=true). This occurs during OverrideMetadata calls or manual metadata merging after the property registration sequence has frozen the metadata.","commonSituations":"Overriding styled/direct property metadata at runtime after the property has been sealed; attempting to call OverrideMetadata more than once; sharing a single metadata instance across multiple AvaloniaProperty registrations and then trying to merge into it again.","solutions":["Ensure Merge/OverrideMetadata is called before the property registration freezes the metadata.","Call GenerateTypeSafeMetadata() to obtain a fresh, writable copy of the metadata and merge into that instead.","If you hold a reference to frozen metadata, create a new AvaloniaPropertyMetadata instance with the desired values rather than mutating the frozen one.","Check metadata.IsReadOnly before calling Merge and skip or copy accordingly."],"exampleFix":"// before (mutates frozen metadata)\nproperty.GetMetadata(ownerType).Merge(baseMetadata, property);\n\n// after (work on a fresh copy)\nif (metadata.IsReadOnly) {\n    var copy = metadata.GenerateTypeSafeMetadata();\n    copy.Merge(baseMetadata, property);\n}","handlingStrategy":"validation","validationCode":"if (metadata.IsReadOnly) { /* use GenerateTypeSafeMetadata() copy instead */ }","typeGuard":"static bool IsMetadataWritable(AvaloniaPropertyMetadata m) => !m.IsReadOnly;","tryCatchPattern":null,"preventionTips":["Always check AvaloniaPropertyMetadata.IsReadOnly before calling Merge.","Prefer OverrideMetadata at registration time, before the property freezes its metadata.","Use GenerateTypeSafeMetadata() to obtain a writable copy instead of mutating shared metadata."],"tags":["avalonia","metadata","immutability","property-system"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}