{"record":{"id":"d22ce8023a15e3e3","repo":"unoplatform/uno","slug":"item-in-the-array-must-be-an-instance-of-0","errorCode":null,"errorMessage":"Item in the array must be an instance of '{0}'","messagePattern":"Item in the array must be an instance of '(.+?)'","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/SourceGenerators/System.Xaml/System.Windows.Markup/ArrayExtension.cs","lineNumber":109,"sourceCode":"\t\t\t\tthrow new InvalidOperationException (\"Type property must be set before calling ProvideValue method\");\n\t\t\t}\n\n\t\t\tbool invalid = false;\n\t\t\tforeach (var item in Items) {\n\t\t\t\tif (item == null) {\n\t\t\t\t\tif (Type.IsValueType)\n\t\t\t\t\t{\n\t\t\t\t\t\tinvalid = true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if (!Type.IsAssignableFrom (item.GetType ()))\n\t\t\t\t{\n\t\t\t\t\tinvalid = true;\n\t\t\t\t}\n\n\t\t\t\tif (invalid)\n\t\t\t\t{\n\t\t\t\t\tthrow new InvalidOperationException (String.Format (CultureInfo.InvariantCulture, \"Item in the array must be an instance of '{0}'\", Type));\n\t\t\t\t}\n\t\t\t}\n\t\t\tArray a = Array.CreateInstance (Type, Items.Count);\n\t\t\tItems.CopyTo (a, 0);\n\t\t\treturn a;\n\t\t}\n\t}\n}\n","sourceCodeStart":91,"sourceCodeEnd":118,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/SourceGenerators/System.Xaml/System.Windows.Markup/ArrayExtension.cs#L91-L118","documentation":"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}.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure every item added is assignable to Type; validate item.GetType().IsAssignableTo(Type) before adding.","Reject null items when Type.IsValueType, or substitute a default value.","In XAML, make sure each child element's type matches the declared x:Array Type.","If heterogeneous values are expected, widen Type or use object."],"exampleFix":"// before\nvar ext = new ArrayExtension(typeof(int));\next.Items.Add(\"not an int\");\next.ProvideValue(sp);\n\n// after\nvar ext = new ArrayExtension(typeof(int));\nforeach (var v in source) if (v is int i) ext.Items.Add(i);\next.ProvideValue(sp);","handlingStrategy":"validation","validationCode":"static bool ItemsAssignable(ArrayExtension ext) => ext.Items.Cast<object>().All(i => i is null ? !ext.Type.IsValueType : ext.Type.IsAssignableFrom(i.GetType()));","typeGuard":"static bool ItemMatches(object item, Type t) => item is null ? !t.IsValueType : t.IsAssignableFrom(item.GetType());","tryCatchPattern":"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; }","preventionTips":["Filter items by assignability before adding to Items.","Reject null for value-type element types.","Keep x:Array children type-consistent with the declared Type in XAML."],"tags":["xaml","markup-extension","x-array","type-mismatch","invalidoperation","system-xaml"],"backgroundTag":null,"analyzedSha":"04183404888ea21b4e5bfa3493e8d5f15e972c3a","analyzedAt":"2026-08-13T21:08:11.651Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}