unoplatform/uno · error · InvalidOperationException

Type property must be set before calling ProvideValue method

Error message

Type property must be set before calling ProvideValue method

What it means

InvalidOperationException thrown by ArrayExtension.ProvideValue (System.Xaml, ported from Mono) when ProvideValue is called before the Type property has been set. x:ArrayExtension needs an element type to construct the array, so calling the markup extension's value provider without a type is a usage error. Typically Type is set via the x:Type attribute or the arrayType constructor argument in markup.

Source

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

		}

		public void AddChild (Object value)
		{
			// null is allowed.
			Items.Add (value);
		}
		
		public void AddText (string text)
		{
			// null is allowed.
			Items.Add (text);
		}
		
		public override object ProvideValue (IServiceProvider serviceProvider)
		{
			if (Type == null)
			{
				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));

View on GitHub (pinned to 0418340488)

Solutions

  1. Set Type (via constructor or the Type property) before calling ProvideValue.
  2. In XAML, always specify the element type, e.g. <x:Array x:Key="..." Type="x:String">.
  3. Add a guard in code: if (ext.Type == null) ext.Type = typeof(...); before ProvideValue.

Example fix

// before
var ext = new ArrayExtension();
ext.Items.Add("a");
var arr = ext.ProvideValue(sp); // Type is null

// after
var ext = new ArrayExtension(typeof(string));
ext.Items.Add("a");
var arr = ext.ProvideValue(sp);
Defensive patterns

Strategy: validation

Validate before calling

static object SafeProvide(ArrayExtension ext, IServiceProvider sp) { if (ext.Type is null) throw new InvalidOperationException("Set ArrayExtension.Type first."); return ext.ProvideValue(sp); }

Type guard

static bool IsReady(ArrayExtension ext) => ext.Type is not null;

Try / catch

try { return ext.ProvideValue(sp); } catch (InvalidOperationException ex) when (ex.Message.Contains("Type property must be set")) { ext.Type = typeof(object); return ext.ProvideValue(sp); }

Prevention

When it happens

Trigger: Programmatically constructing ArrayExtension() (parameterless) and calling ProvideValue without setting Type; or XAML that omits the x:Type element type.

Common situations: Omitting the Type attribute on x:ArrayExtension in XAML, manually invoking ProvideValue in tests/code without configuring Type, or a code path that clears Type before evaluation.

Related errors


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