{"record":{"id":"f31b576b4b9cc5be","repo":"dotnet/wpf","slug":"sr-format-sr-typemetadataalreadyregistered-fortype-name","errorCode":null,"errorMessage":"SR.Format(SR.TypeMetadataAlreadyRegistered, forType.Name)","messagePattern":"SR\\.Format\\(SR\\.TypeMetadataAlreadyRegistered, forType\\.Name\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyProperty.cs","lineNumber":566,"sourceCode":"        /// method is called to actually update the data structures.\n        /// </summary>\n        private void ProcessOverrideMetadata(\n            Type forType,\n            PropertyMetadata typeMetadata,\n            DependencyObjectType dType,\n            PropertyMetadata baseMetadata)\n        {\n            // Store per-Type metadata for this property. Locks only on Write.\n            // Datastructure guaranteed to be valid for non-locking readers\n            lock (Synchronized)\n            {\n                if (DependencyProperty.UnsetValue == _metadataMap[dType.Id])\n                {\n                    _metadataMap[dType.Id] = typeMetadata;\n                }\n                else\n                {\n                    throw new ArgumentException(SR.Format(SR.TypeMetadataAlreadyRegistered, forType.Name));\n                }\n           }\n\n            // Merge base's metadata into this metadata\n            // CALLBACK\n            typeMetadata.InvokeMerge(baseMetadata, this);\n\n            // Type metadata may no longer change (calls OnApply)\n            typeMetadata.Seal(this, forType);\n\n            if (typeMetadata.IsInherited)\n            {\n                _packedData |= Flags.IsPotentiallyInherited;\n            }\n\n            if (typeMetadata.DefaultValueWasSet() && (typeMetadata.DefaultValue != DefaultMetadata.DefaultValue))\n            {\n                _packedData |= Flags.IsDefaultValueChanged;","sourceCodeStart":548,"sourceCodeEnd":584,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyProperty.cs#L548-L584","documentation":"ProcessOverrideMetadata throws this ArgumentException when metadata for the given type has already been registered for this DependencyProperty (the _metadataMap slot for the type's Id is not UnsetValue). Each type may have its metadata overridden at most once per property.","triggerScenarios":"Calling OverrideMetadata twice for the same (property, forType) pair; calling it again in a static constructor that runs more than once via reflection/re-invocation; two base classes in a hierarchy both overriding metadata for the same intermediate type.","commonSituations":"Duplicate static constructors in partial classes both overriding metadata; merging code from two libraries that both override metadata of the same DP for the same type; unit tests re-registering metadata repeatedly.","solutions":["Remove the duplicate OverrideMetadata call so each (property, type) pair is overridden exactly once","Guard the call with a static bool 'metadataInitialized' flag to make it idempotent","If two sources must both customize metadata, merge their values into a single FrameworkPropertyMetadata and register it in one call"],"exampleFix":"// before\nstatic MyControl()\n{\n    FooProperty.OverrideMetadata(typeof(MyControl), new PropertyMetadata(1));\n}\nstatic MyControl() // partial class: runs duplicate registration\n{\n    FooProperty.OverrideMetadata(typeof(MyControl), new PropertyMetadata(2)); // throws\n}\n// after\nprivate static bool _initialized;\nstatic MyControl()\n{\n    if (_initialized) return;\n    _initialized = true;\n    FooProperty.OverrideMetadata(typeof(MyControl), new PropertyMetadata(2));\n}","handlingStrategy":"validation","validationCode":"private static bool _metaDone;\nif (!_metaDone)\n{\n    dp.OverrideMetadata(typeof(MyControl), meta);\n    _metaDone = true;\n}","typeGuard":null,"tryCatchPattern":"try { dp.OverrideMetadata(typeof(MyControl), meta); }\ncatch (ArgumentException) { /* metadata already registered: treat as idempotent no-op */ }","preventionTips":["Override metadata exactly once per (DP, type), ideally in the type's static constructor","Use a static bool guard in partial classes so duplicate static ctors cannot double-register","Search the solution for OverrideMetadata on the same property before adding a new call"],"tags":["wpf","dependency-property","duplicate-registration","metadata-override","argument"],"backgroundTag":"file-already-exists","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"}