unoplatform/uno · error · InvalidOperationException

Item in the array must be an instance of '{0}'

Error message

Item in the array must be an instance of '{0}'

What it means

InvalidOperationException thrown by ArrayExtension.ProvideValue (System.Xaml, ported from Mono) when an item in Items is not assignable to the configured element Type. The extension builds a typed Array.CreateInstance(Type, ...), so every item must be assignable to Type; a null item for a value type, or a wrong-typed reference, is rejected. The message formats Type into slot {0}.

Source

Thrown at src/SourceGenerators/System.Xaml/System.Windows.Markup/ArrayExtension.cs:109

				throw new InvalidOperationException ("Type property must be set before calling ProvideValue method");
			}

			bool invalid = false;
			foreach (var item in Items) {
				if (item == null) {
					if (Type.IsValueType)
					{
						invalid = true;
					}
				}
				else if (!Type.IsAssignableFrom (item.GetType ()))
				{
					invalid = true;
				}

				if (invalid)
				{
					throw new InvalidOperationException (String.Format (CultureInfo.InvariantCulture, "Item in the array must be an instance of '{0}'", Type));
				}
			}
			Array a = Array.CreateInstance (Type, Items.Count);
			Items.CopyTo (a, 0);
			return a;
		}
	}
}

View on GitHub (pinned to 0418340488)

Solutions

  1. Ensure every item added is assignable to Type; validate item.GetType().IsAssignableTo(Type) before adding.
  2. Reject null items when Type.IsValueType, or substitute a default value.
  3. In XAML, make sure each child element's type matches the declared x:Array Type.
  4. If heterogeneous values are expected, widen Type or use object.

Example fix

// before
var ext = new ArrayExtension(typeof(int));
ext.Items.Add("not an int");
ext.ProvideValue(sp);

// after
var ext = new ArrayExtension(typeof(int));
foreach (var v in source) if (v is int i) ext.Items.Add(i);
ext.ProvideValue(sp);
Defensive patterns

Strategy: validation

Validate before calling

static bool ItemsAssignable(ArrayExtension ext) => ext.Items.Cast<object>().All(i => i is null ? !ext.Type.IsValueType : ext.Type.IsAssignableFrom(i.GetType()));

Type guard

static bool ItemMatches(object item, Type t) => item is null ? !t.IsValueType : t.IsAssignableFrom(item.GetType());

Try / catch

try { return ext.ProvideValue(sp); } catch (InvalidOperationException ex) when (ex.Message.Contains("Item in the array")) { log.Error("x:Array item type mismatch: {0}", ext.Type); throw; }

Prevention

When it happens

Trigger: Adding an item whose runtime type is not assignable to Type (e.g. a string into an int array), or adding null when Type is a value type.

Common situations: Mixed-type items added programmatically to Items, a null entry in a value-type array, XAML x:Array children whose types do not match the declared Type, or a binding producing a wrong-typed value.

Related errors


AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13). Data as JSON: /api/errors/d22ce8023a15e3e3. Report an issue: GitHub.