{"record":{"id":"6fc00e63faf7722d","repo":"dotnet/wpf","slug":"sr-markupextensionarraytype","errorCode":null,"errorMessage":"SR.MarkupExtensionArrayType","messagePattern":"SR\\.MarkupExtensionArrayType","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/ArrayExtension.cs","lineNumber":84,"sourceCode":"        [ConstructorArgument(\"type\")]\n        public Type Type { get; set; }\n\n        /// <summary>\n        /// An IList accessor to the contents of the array\n        /// </summary>\n        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]\n        public IList Items => _arrayList;\n\n        /// <summary>\n        /// Return an array that is sized to the number of objects added to the ArrayExtension.\n        /// </summary>\n        /// <param name=\"serviceProvider\">Object that can provide services for the markup extension.</param>\n        /// <returns>The Array containing all the objects added to this extension.</returns>\n        public override object ProvideValue(IServiceProvider serviceProvider)\n        {\n            if (Type is null)\n            {\n                throw new InvalidOperationException(SR.MarkupExtensionArrayType);\n            }\n\n            try\n            {\n                return _arrayList.ToArray(Type);\n            }\n            catch (InvalidCastException)\n            {\n                // If an element was added to the ArrayExtension that does not agree with the\n                // ArrayType, then an InvalidCastException will occur.\n                // Generate a more meaningful error for this case.\n                throw new InvalidOperationException(SR.Format(SR.MarkupExtensionArrayBadType, Type.Name));\n            }\n        }\n    }\n}\n","sourceCodeStart":66,"sourceCodeEnd":101,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/ArrayExtension.cs#L66-L101","documentation":"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.","triggerScenarios":"Calling ProvideValue on an ArrayExtension created with the parameterless constructor where Type was never set (or was set to null).","commonSituations":"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.","solutions":["Set the Type property (or use the ArrayExtension(Type) constructor) before calling ProvideValue.","Wrap ProvideValue in a check: if extension.Type is null, throw/handle before invoking.","If parsing XAML, ensure the x:Type attribute on the x:Array element is present and resolves."],"exampleFix":"// before\nvar ext = new ArrayExtension();\nvar arr = ext.ProvideValue(provider); // throws\n// after\nvar ext = new ArrayExtension(typeof(int));\next.AddChild(1);\nvar arr = ext.ProvideValue(provider);","handlingStrategy":"type-guard","validationCode":"if (ext.Type is null) throw new InvalidOperationException(\"ArrayExtension.Type must be set before ProvideValue\");","typeGuard":"bool CanProvideValue(ArrayExtension ext) => ext?.Type is not null;","tryCatchPattern":"try { var arr = ext.ProvideValue(provider); }\ncatch (InvalidOperationException) { /* Type not initialized — recover or rethrow with context */ }","preventionTips":["Always construct ArrayExtension with the Type-taking constructor when the type is known.","In XAML, never omit x:Type on x:Array elements.","Add an assertion/debug check that Type is set before pipeline stages that call ProvideValue."],"tags":["invalid-state","xaml","markup-extension","uninitialized"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}