{"record":{"id":"f296b1d2b95b629e","repo":"dotnet/wpf","slug":"sr-overridingmetadatadoesnotmatchbasemetadatatype","errorCode":null,"errorMessage":"SR.OverridingMetadataDoesNotMatchBaseMetadataType","messagePattern":"SR\\.OverridingMetadataDoesNotMatchBaseMetadataType","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyProperty.cs","lineNumber":482,"sourceCode":"            // otherwise, the default value will be taken from the base metadata\n            // which was already validated)\n            if (typeMetadata.IsDefaultValueModified)\n            {\n                // Will throw ArgumentException if fails.\n                ValidateMetadataDefaultValue( typeMetadata, PropertyType, Name, ValidateValueCallback );\n            }\n\n            // Force all base classes to register their metadata\n            dType = DependencyObjectType.FromSystemType(forType);\n\n            // Get metadata for the base type\n            baseMetadata = GetMetadata(dType.BaseType);\n\n            // Make sure overriding metadata is the same type or derived type of\n            // the base metadata\n            if (!baseMetadata.GetType().IsAssignableFrom(typeMetadata.GetType()))\n            {\n                throw new ArgumentException(SR.OverridingMetadataDoesNotMatchBaseMetadataType);\n            }\n        }\n\n\n        /// <summary>\n        ///     Supply metadata for given type & run static constructors if needed.\n        /// </summary>\n        /// <remarks>\n        ///     The supplied metadata will be merged with the type's base\n        ///     metadata\n        /// </remarks>\n        public void OverrideMetadata(Type forType, PropertyMetadata typeMetadata)\n        {\n            DependencyObjectType dType;\n            PropertyMetadata baseMetadata;\n\n            SetupOverrideMetadata(forType, typeMetadata, out dType, out baseMetadata);\n","sourceCodeStart":464,"sourceCodeEnd":500,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyProperty.cs#L464-L500","documentation":"When overriding metadata, the new metadata's type must be the same as, or derived from, the base metadata's type. You cannot replace a specialized metadata (e.g. FrameworkPropertyMetadata) with a less-derived one (e.g. PropertyMetadata), because that would drop behavior the property system relies on.","triggerScenarios":"Calling OverrideMetadata with new PropertyMetadata(...) on a property whose base metadata is FrameworkPropertyMetadata; also overriding with a metadata type from an unrelated hierarchy.","commonSituations":"Copy-pasting a generic PropertyMetadata override for a UI property declared with FrameworkPropertyMetadata; writing helper methods that always construct plain PropertyMetadata.","solutions":["Use the same metadata type as the base (typically FrameworkPropertyMetadata for UI element properties).","Derive your override metadata type from the base metadata type rather than from PropertyMetadata.","Inspect the base property's DefaultMetadata type to determine the correct class to use."],"exampleFix":"// before\nMyProperty.OverrideMetadata(typeof(MyControl), new PropertyMetadata(false));\n// after\nMyProperty.OverrideMetadata(typeof(MyControl),\n    new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));","handlingStrategy":"validation","validationCode":"var baseMeta = dp.GetMetadata(typeof(BaseType));\nif (!baseMeta.GetType().IsAssignableFrom(newMeta.GetType())) throw new ArgumentException(\"Override metadata type must derive from base metadata type\");","typeGuard":"static bool IsCompatibleMetadata(DependencyProperty dp, Type forType, PropertyMetadata m) => dp.GetMetadata(forType.BaseType).GetType().IsAssignableFrom(m.GetType());","tryCatchPattern":null,"preventionTips":["Match the base metadata type (usually FrameworkPropertyMetadata) when overriding","Check DefaultMetadata type of the property before constructing override metadata"],"tags":["wpf","dependency-property","metadata"],"backgroundTag":"incompatible-source-type","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"}