{"record":{"id":"3b07a2afaf183a4a","repo":"dotnet/wpf","slug":"sr-changingidnotallowed","errorCode":null,"errorMessage":"SR.ChangingIdNotAllowed","messagePattern":"SR\\.ChangingIdNotAllowed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ComponentResourceKey.cs","lineNumber":86,"sourceCode":"                return _typeInTargetAssembly?.Assembly;\n            }\n        }\n\n        /// <summary>\n        ///     A unique Id to differentiate this key from other keys associated with the same type.\n        /// </summary>\n        public object ResourceId\n        {\n            get\n            {\n                return _resourceId;\n            }\n\n            set\n            {\n                if (_resourceIdInitialized)\n                {\n                    throw new InvalidOperationException(SR.ChangingIdNotAllowed);\n                }\n                _resourceId = value;\n                _resourceIdInitialized = true;\n            }\n        }\n\n        /// <summary>\n        ///     Determines if the passed in object is equal to this object.\n        ///     Two keys will be equal if they both have equal Types and IDs.\n        /// </summary>\n        /// <param name=\"o\">The object to compare with.</param>\n        /// <returns>True if the objects are equal. False otherwise.</returns>\n        public override bool Equals(object o)\n        {\n            ComponentResourceKey key = o as ComponentResourceKey;\n\n            if (key != null)\n            {","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ComponentResourceKey.cs#L68-L104","documentation":"ComponentResourceKey.ResourceId is likewise write-once: once _resourceIdInitialized is true, a second assignment throws InvalidOperationException. The key's identity must remain stable once created so resource dictionary lookups are consistent.","triggerScenarios":"Assigning ComponentResourceKey.ResourceId when it was already initialized (via constructor or a previous set).","commonSituations":"Mutating a shared/static key instance to point at a different resource id; XAML defining the ResourceId then code-behind reassigning it.","solutions":["Create a new ComponentResourceKey with the desired ResourceId","Set ResourceId exactly once, at construction","Track initialization before assigning"],"exampleFix":"// before\nsharedKey.ResourceId = \"OtherId\"; // throws\n// after\nvar newKey = new ComponentResourceKey(typeof(MyClass), \"OtherId\");","handlingStrategy":"validation","validationCode":"if (key.ResourceId == null) { key.ResourceId = id; } else { key = new ComponentResourceKey(key.TypeInTargetAssembly, id); }","typeGuard":null,"tryCatchPattern":"try { key.ResourceId = id; } catch (InvalidOperationException) { key = new ComponentResourceKey(key.TypeInTargetAssembly, id); }","preventionTips":["Always initialize ResourceId in the constructor","Treat the key as immutable after creation"],"tags":["wpf","resources","invalid-operation","immutable-state"],"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"}