dotnet/wpf · error · InvalidOperationException

SR.MarkupExtensionTypeName

Error message

SR.MarkupExtensionTypeName

What it means

TypeExtension.ProvideValue throws InvalidOperationException when neither a Type nor a TypeName string was supplied before evaluation. The extension cannot resolve a type without one of these, so the library fails fast instead of returning a meaningless value. It is the XAML authoring equivalent of an unset required field.

Solutions

  1. Pass a Type to the constructor: new TypeExtension(typeof(MyType)) before calling ProvideValue.
  2. Set the TypeName property to a valid qualified type-name string, e.g. new TypeExtension { TypeName = "sys:String" }.
  3. Set the Type property instead when the CLR type is known at compile time.
  4. Guard the call: only invoke ProvideValue after confirming Type != null || TypeName != null.

Example fix

// before
var ext = new TypeExtension();
var val = ext.ProvideValue(provider); // throws
// after
var ext = new TypeExtension(typeof(System.String));
var val = ext.ProvideValue(provider);
Defensive patterns

Strategy: validation

Validate before calling

if (ext.Type is null && ext.TypeName is null)
    throw new InvalidOperationException("TypeExtension requires Type or TypeName before ProvideValue.");

Type guard

bool IsInitialized(TypeExtension e) => e is { Type: not null } || e is { TypeName: not null };

Try / catch

try { value = ext.ProvideValue(provider); }
catch (InvalidOperationException ex) { log.Warn(ex); value = Type.GetType(ext.TypeName ?? "", throwOnError: false); }

Prevention

When it happens

Trigger: Calling new TypeExtension().ProvideValue(serviceProvider) without calling the TypeExtension(Type) constructor, without setting Type, and without setting TypeName.

Common situations: Hand-constructing TypeExtension in code (rather than from XAML markup) and forgetting to set Type or TypeName; data-binding or tooling code that builds markup extensions dynamically and passes incomplete initialization state.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/TypeExtension.cs:63

        /// <summary>
        /// Return an object that should be set on the targetObject's targetProperty
        /// for this markup extension. TypeExtension resolves a string into a Type
        /// and returns it.
        /// </summary>
        /// <param name="serviceProvider">Object that can provide services for the markup extension.</param>
        /// <returns>The object to set on this property.</returns>
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            // If a type was supplied, no context nor type name are needed
            if (_type is not null)
            {
                return _type;
            }

            // Validate the initialization.
            if (_typeName is null)
            {
                throw new InvalidOperationException(SR.MarkupExtensionTypeName);
            }

            // Get the IXamlTypeResolver from the service provider
            ArgumentNullException.ThrowIfNull(serviceProvider);

            if (serviceProvider.GetService(typeof(IXamlTypeResolver)) is not IXamlTypeResolver xamlTypeResolver)
            {
                throw new InvalidOperationException(SR.Format(SR.MarkupExtensionNoContext, GetType().Name, nameof(IXamlTypeResolver)));
            }

            // Get the type
            _type = xamlTypeResolver.Resolve(_typeName);
            if (_type is null)
            {
                throw new InvalidOperationException(SR.Format(SR.MarkupExtensionTypeNameBad, _typeName));
            }

            return _type;

View on GitHub (pinned to 81131a70a4)