{"record":{"id":"ab62a4a578edf837","repo":"dotnet/wpf","slug":"sr-format-sr-markupextensionarraybadtype-type-name","errorCode":null,"errorMessage":"SR.Format(SR.MarkupExtensionArrayBadType, Type.Name)","messagePattern":"SR\\.Format\\(SR\\.MarkupExtensionArrayBadType, Type\\.Name\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/ArrayExtension.cs","lineNumber":96,"sourceCode":"        /// <param name=\"serviceProvider\">Object that can provide services for the markup extension.</param>\n        /// <returns>The Array containing all the objects added to this extension.</returns>\n        public override object ProvideValue(IServiceProvider serviceProvider)\n        {\n            if (Type is null)\n            {\n                throw new InvalidOperationException(SR.MarkupExtensionArrayType);\n            }\n\n            try\n            {\n                return _arrayList.ToArray(Type);\n            }\n            catch (InvalidCastException)\n            {\n                // If an element was added to the ArrayExtension that does not agree with the\n                // ArrayType, then an InvalidCastException will occur.\n                // Generate a more meaningful error for this case.\n                throw new InvalidOperationException(SR.Format(SR.MarkupExtensionArrayBadType, Type.Name));\n            }\n        }\n    }\n}\n","sourceCodeStart":78,"sourceCodeEnd":101,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/ArrayExtension.cs#L78-L101","documentation":"ArrayExtension.ProvideValue catches InvalidCastException from _arrayList.ToArray(Type) and rethrows it as InvalidOperationException (SR.MarkupExtensionArrayBadType, mentioning Type.Name). This happens when an element added to the extension cannot be cast to the declared array element type. The library wraps the low-level cast failure in a more meaningful error naming the array type.","triggerScenarios":"Adding items to an ArrayExtension whose runtime type is not assignable to the declared Type (e.g. adding a string to an ArrayExtension(typeof(int))), then calling ProvideValue.","commonSituations":"XAML x:Array elements whose children do not match the declared x:Type; data-binding or dynamic content generators producing heterogeneous item lists; schema changes where the declared element type was updated but the item sources were not.","solutions":["Ensure every item added via AddChild matches (or is assignable to) the declared array Type.","Verify the x:Type on the x:Array in the XAML matches the actual child element types.","Wrap ProvideValue in a try/catch for InvalidOperationException and log/inspect the offending items if the content is dynamic."],"exampleFix":"// before\nvar ext = new ArrayExtension(typeof(int));\next.AddChild(\"not-an-int\");\nvar arr = ext.ProvideValue(provider); // throws\n// after\nvar ext = new ArrayExtension(typeof(int));\next.AddChild(42);\nvar arr = ext.ProvideValue(provider);","handlingStrategy":"validation","validationCode":"foreach (var item in items)\n    if (item is not null && !arrayType.IsInstanceOfType(item))\n        throw new InvalidOperationException($\"Item {item} is not assignable to {arrayType}\");","typeGuard":"bool AllItemsMatchType(ArrayExtension ext, Type expected) => ext?.Type == expected && items.All(expected.IsInstanceOfType);","tryCatchPattern":"try { var arr = ext.ProvideValue(provider); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(ext.Type?.Name)) { /* inspect/fix item types */ }","preventionTips":["Validate each AddChild value against the declared array Type at insertion time.","Keep x:Array x:Type declarations in sync with generated content.","Cover heterogeneous-content scenarios with unit tests calling ProvideValue."],"tags":["type-mismatch","xaml","markup-extension","invalid-cast"],"backgroundTag":"type-mismatch","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"}