dotnet/wpf · error · InvalidOperationException
SR.Format(SR.MarkupExtensionArrayBadType, Type.Name)
Error message
SR.Format(SR.MarkupExtensionArrayBadType, Type.Name)
What it means
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.
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.
Example fix
// before
var ext = new ArrayExtension(typeof(int));
ext.AddChild("not-an-int");
var arr = ext.ProvideValue(provider); // throws
// after
var ext = new ArrayExtension(typeof(int));
ext.AddChild(42);
var arr = ext.ProvideValue(provider); Defensive patterns
Strategy: validation
Validate before calling
foreach (var item in items)
if (item is not null && !arrayType.IsInstanceOfType(item))
throw new InvalidOperationException($"Item {item} is not assignable to {arrayType}"); Type guard
bool AllItemsMatchType(ArrayExtension ext, Type expected) => ext?.Type == expected && items.All(expected.IsInstanceOfType);
Try / catch
try { var arr = ext.ProvideValue(provider); }
catch (InvalidOperationException ex) when (ex.Message.Contains(ext.Type?.Name)) { /* inspect/fix item types */ } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- ArgumentException(SR.XamlXmlWriterCannotWriteNonstringValue…
- ArgumentNullException(nameof(arrayType))
- ArgumentNullException(nameof(member))
- brokenRule (SR.UnexpectedToken-based parser rule error)
- IAmbientProvider
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/ab62a4a578edf837.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/ArrayExtension.cs:96
/// <param name="serviceProvider">Object that can provide services for the markup extension.</param>
/// <returns>The Array containing all the objects added to this extension.</returns>
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (Type is null)
{
throw new InvalidOperationException(SR.MarkupExtensionArrayType);
}
try
{
return _arrayList.ToArray(Type);
}
catch (InvalidCastException)
{
// If an element was added to the ArrayExtension that does not agree with the
// ArrayType, then an InvalidCastException will occur.
// Generate a more meaningful error for this case.
throw new InvalidOperationException(SR.Format(SR.MarkupExtensionArrayBadType, Type.Name));
}
}
}
}
View on GitHub (pinned to 81131a70a4)