{"record":{"id":"edd1752551bfa4de","repo":"dotnet/wpf","slug":"sr-format-sr-unexpectedconstructorarg-cad-constructor","errorCode":null,"errorMessage":"SR.Format(SR.UnexpectedConstructorArg, cad.Constructor.DeclaringType, Member, expectedCount, expectedType)","messagePattern":"SR\\.Format\\(SR\\.UnexpectedConstructorArg, cad\\.Constructor\\.DeclaringType, Member, expectedCount, expectedType\\)","errorType":"exception","errorClass":"XamlSchemaException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/Reflector.cs","lineNumber":544,"sourceCode":"\n            return null;\n        }\n\n        private void GetAttributes(Type attributeType, IList<CustomAttributeData> cads)\n        {\n            EnsureAttributeData();\n            for (int i = 0; i < _attributeData.Count; i++)\n            {\n                if (TypesAreEqual(_attributeData[i].Constructor.DeclaringType, attributeType))\n                {\n                    cads.Add(_attributeData[i]);\n                }\n            }\n        }\n\n        protected void ThrowInvalidMetadata(CustomAttributeData cad, int expectedCount, Type expectedType)\n        {\n            throw new XamlSchemaException(SR.Format(SR.UnexpectedConstructorArg,\n                cad.Constructor.DeclaringType, Member, expectedCount, expectedType));\n        }\n    }\n}\n","sourceCodeStart":526,"sourceCodeEnd":549,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/Reflector.cs#L526-L549","documentation":"Reflector.ThrowInvalidMetadata throws XamlSchemaException when a custom attribute's constructor arguments do not match what the XAML metadata reader expects. The message reports the attribute's declaring type, the member being reflected, the expected argument count, and the expected argument type. It guards against malformed or hand-crafted attribute metadata.","triggerScenarios":"Called from ExtractType/ExtractTypes/Extract when parsing a CustomAttributeData whose ConstructorInvoke arguments have the wrong count or wrong type for the attribute kind being extracted (e.g. an attribute expecting one Type argument receives an int, or receives two arguments instead of one).","commonSituations":"Building attributes via reflection emit or IL weavers with wrong constructor signatures; referencing an attribute overload that doesn't match the XAML metadata contract; assembly compiled against a different System.Xaml version where the attribute constructor changed.","solutions":["Fix the attribute usage at the member named in the message so its constructor arguments match the expected count/type.","Rebuild the assembly so attribute metadata is emitted by the normal compiler instead of post-processing tools.","Align the referenced System.Xaml/WPF version with the one the attributes were compiled against."],"exampleFix":"// before\n[XmlnsDefinition(\"http://example/ns\")] // missing second (assembly) argument\npublic class Foo { }\n\n// after\n[XmlnsDefinition(\"http://example/ns\", \"MyAssembly\")]\npublic class Foo { }","handlingStrategy":"validation","validationCode":"// verify attribute constructor args before extraction\nvar cad = attributeData;\nif (cad.ConstructorArguments.Count != expectedCount ||\n    !expectedType.IsInstanceOfType(cad.ConstructorArguments[0].Value))\n    throw new InvalidOperationException($\"{cad.Constructor.DeclaringType}: bad ctor args for {member.Name}\");","typeGuard":"bool HasValidCtorArgs(CustomAttributeData cad, int count, Type t) => cad.ConstructorArguments.Count == count && cad.ConstructorArguments.All(a => t.IsInstanceOfType(a.Value));","tryCatchPattern":"try { types = Reflector.ExtractTypes(member); }\ncatch (XamlSchemaException ex) { log($\"Bad attribute metadata: {ex.Message}\"); /* rebuild assembly or fix attribute */ }","preventionTips":["Apply attributes only via normal C#/VB syntax, not reflection emit","Match the documented constructor overload exactly","Rebuild assemblies after upgrading System.Xaml/WPF versions","Audit IL-weaving/post-build tools that rewrite attribute metadata"],"tags":["xaml","metadata","attribute"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}