dotnet/wpf · error · InvalidOperationException

SR.MarkupExtensionArrayType

Error message

SR.MarkupExtensionArrayType

What it means

ArrayExtension.ProvideValue throws InvalidOperationException (SR.MarkupExtensionArrayType) when the extension's Type property is still null. The extension was constructed without a type and ProvideValue cannot materialize the array. The library requires the array type to be known before the extension produces its value.

Solutions

  1. Set the Type property (or use the ArrayExtension(Type) constructor) before calling ProvideValue.
  2. Wrap ProvideValue in a check: if extension.Type is null, throw/handle before invoking.
  3. If parsing XAML, ensure the x:Type attribute on the x:Array element is present and resolves.

Example fix

// before
var ext = new ArrayExtension();
var arr = ext.ProvideValue(provider); // throws
// after
var ext = new ArrayExtension(typeof(int));
ext.AddChild(1);
var arr = ext.ProvideValue(provider);
Defensive patterns

Strategy: type-guard

Validate before calling

if (ext.Type is null) throw new InvalidOperationException("ArrayExtension.Type must be set before ProvideValue");

Type guard

bool CanProvideValue(ArrayExtension ext) => ext?.Type is not null;

Try / catch

try { var arr = ext.ProvideValue(provider); }
catch (InvalidOperationException) { /* Type not initialized — recover or rethrow with context */ }

Prevention

When it happens

Trigger: Calling ProvideValue on an ArrayExtension created with the parameterless constructor where Type was never set (or was set to null).

Common situations: Programmatic XAML object graph construction where the x:Array extension is instantiated but the x:Type attribute / Type property assignment is skipped; hand-built markup extension pipelines missing an initialization step.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/6fc00e63faf7722d. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/ArrayExtension.cs:84

        [ConstructorArgument("type")]
        public Type Type { get; set; }

        /// <summary>
        /// An IList accessor to the contents of the array
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public IList Items => _arrayList;

        /// <summary>
        /// Return an array that is sized to the number of objects added to the ArrayExtension.
        /// </summary>
        /// <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)