dotnet/wpf · error · ArgumentException

SR.Format(SR.MustBeOfType, nameof(value)…

Error message

SR.Format(SR.MustBeOfType, nameof(value), nameof(TypeExtension))

What it means

TypeExtensionConverter.ConvertTo throws ArgumentException when the value being converted is not a TypeExtension instance. The converter can only produce an InstanceDescriptor from an actual TypeExtension; any other value fails the type check with SR.MustBeOfType naming the parameter and expected type.

Solutions

  1. Pass the TypeExtension instance itself, not its underlying Type.
  2. Check value is TypeExtension before calling ConvertTo.
  3. If you have a Type, wrap it: new TypeExtension(theType) then convert.
  4. Use the correct converter for the value's actual runtime type.

Example fix

// before
var desc = converter.ConvertTo(null, culture, typeof(string), typeof(InstanceDescriptor)); // throws
// after
var ext = new TypeExtension(typeof(string));
var desc = converter.ConvertTo(null, culture, ext, typeof(InstanceDescriptor));
Defensive patterns

Strategy: type-guard

Validate before calling

if (value is not TypeExtension) throw new ArgumentException($"Expected TypeExtension, got {value?.GetType().Name ?? "null"}.");

Type guard

bool IsTypeExtension(object? v) => v is TypeExtension;

Try / catch

try { desc = converter.ConvertTo(ctx, culture, value, typeof(InstanceDescriptor)); }
catch (ArgumentException ex) { log.Error(ex); desc = null; }

Prevention

When it happens

Trigger: Calling TypeConverter.ConvertTo(value, typeof(InstanceDescriptor)) where value is null or an object that is not TypeExtension — e.g. passing the raw Type instead of the extension wrapper.

Common situations: Custom designers/codegen calling the converter with the wrong object; serialization pipelines that pass property values of mixed types through a converter registered for TypeExtension.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/TypeExtensionConverter.cs:32

#pragma warning restore CA1812
    {
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(InstanceDescriptor))
            {
                return true;
            }

            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(InstanceDescriptor))
            {
                if (!(value is TypeExtension typeExtension))
                {
                    throw new ArgumentException(SR.Format(SR.MustBeOfType, nameof(value), nameof(TypeExtension)));
                }

                return new InstanceDescriptor(
                    typeof(TypeExtension).GetConstructor(new Type[] { typeof(Type) }),
                    new object[] { typeExtension.Type }
                );
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }
    }
}

View on GitHub (pinned to 81131a70a4)