{"record":{"id":"504155dfd1ef9939","repo":"dotnet/wpf","slug":"can-t-assign-to-known-type-attributes","errorCode":null,"errorMessage":"Can't Assign to Known Type attributes","messagePattern":"Can't Assign to Known Type attributes","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/WpfKnownType.cs","lineNumber":93,"sourceCode":"            bool isBamlType,\n            bool useV3Rules)\n            : base(underlyingType, schema, isBamlType, useV3Rules)\n        {\n            _bamlNumber = (short)bamlNumber;\n            _name = name;\n            _underlyingType = underlyingType;\n        }\n\n        public void Freeze()\n        {\n            Frozen = true;\n        }\n\n        private void CheckFrozen()\n        {\n            if (Frozen)\n            {\n                throw new InvalidOperationException(\"Can't Assign to Known Type attributes\");\n            }\n        }\n\n        public short BamlNumber\n        {\n            get { return _bamlNumber; }\n        }\n\n        protected override XamlMember LookupContentProperty()\n        {\n            return CallGetMember(_contentPropertyName);\n        }\n\n        public string ContentPropertyName\n        {\n            get { return _contentPropertyName; }\n            set\n            {","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/WpfKnownType.cs#L75-L111","documentation":"WpfKnownType caches BAML-known type metadata (attributes, content property names, converters) and is frozen once the shared schema context is fully initialized. CheckFrozen guards any late mutation of that metadata. This InvalidOperationException means code attempted to assign a known-type attribute after the type was frozen, which the library treats as an internal invariant violation rather than a user error.","triggerScenarios":"Setting BamlNumber, ContentPropertyName, RuntimeNamePropertyName, XmlLangPropertyName, UsableDuringInitialization, or a value converter on a WpfKnownType after Frozen became true — i.e. mutating known-type metadata outside schema-context initialization (typically only from custom subclasses of WpfKnownType or reflection hacks).","commonSituations":"Custom Baml2006 schema-context/known-type subclasses that populate attributes lazily after the context sealed its tables; reflective tooling or tests that mutate known types post-initialization; concurrency bugs where a thread assigns metadata after another thread froze it.","solutions":["Populate all known-type attributes before the schema context freezes its tables (during construction/initialization), not lazily afterward.","If you subclass WpfKnownType, compute values in overridden Lookup* methods instead of assigning frozen properties.","Check the Frozen property before assigning and skip/warn instead of mutating.","Verify no thread race: freeze on one thread only after all initialization completes."],"exampleFix":"// before\nknownType.ContentPropertyName = \"Children\"; // after context is frozen\n// after\nvar knownType = new WpfKnownType(..., contentPropertyName: \"Children\", ...); // set at construction, before freezing","handlingStrategy":"validation","validationCode":"if (knownType.Frozen)\n    throw new InvalidOperationException(\"Known type is frozen; set attributes during initialization.\");","typeGuard":"bool CanMutate(WpfKnownType t) => !t.Frozen;","tryCatchPattern":null,"preventionTips":["Set all known-type attributes at construction/initialization time.","Prefer Lookup* overrides over post-freeze assignment in subclasses.","Freeze the schema context only after all metadata is populated.","Avoid mutating shared schema-context state from multiple threads."],"tags":["wpf","baml","xaml","frozen-state","schema-context"],"backgroundTag":"invalid-state-transition","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"}