dotnet/wpf · error · ArgumentException

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

Error message

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

What it means

StaticExtensionsToInstanceDescriptorsConverter.ConvertTo only converts a StaticExtension value into an InstanceDescriptor. If the value passed to ConvertTo is not a StaticExtension, it throws ArgumentException(SR.MustBeOfType), reporting the value parameter must be of type StaticExtension.

Solutions

  1. Pass only StaticExtension instances to this converter.
  2. Type-check (value is StaticExtension) before invoking ConvertTo.
  3. Use the converter registered for the actual value's type instead.

Example fix

// before
var descriptor = converter.ConvertTo(null, culture, typeExtension, typeof(InstanceDescriptor));
// after
if (value is StaticExtension staticExtension)
    var descriptor = converter.ConvertTo(null, culture, staticExtension, typeof(InstanceDescriptor));
Defensive patterns

Strategy: type-guard

Validate before calling

if (value is not StaticExtension) throw new ArgumentException($"ConvertTo(InstanceDescriptor) requires a StaticExtension, got {value?.GetType().Name ?? "null"}");

Type guard

static bool IsStaticExtension(object? v) => v is StaticExtension;

Try / catch

try { result = converter.ConvertTo(ctx, culture, value, typeof(InstanceDescriptor)); } catch (ArgumentException ex) { /* wrong value type for this converter */ }

Prevention

When it happens

Trigger: Calling converter.ConvertTo(context, culture, someOtherObject, typeof(InstanceDescriptor)) with a value that is not StaticExtension — e.g. a TypeExtension or a raw string.

Common situations: Custom designer/serializer pipelines that feed mixed markup-extension values into the converter; code that assumes the converter accepts any markup extension.

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/8bb84566a74ca55b. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/StaticExtensionsToInstanceDescriptorsConverter.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 StaticExtension staticExtension))
                {
                    throw new ArgumentException(SR.Format(SR.MustBeOfType, nameof(value), nameof(StaticExtension)));
                }

                return new InstanceDescriptor(
                    typeof(StaticExtension).GetConstructor(new Type[] { typeof(string) }),
                    new object[] { staticExtension.Member }
                );
            }

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

View on GitHub (pinned to 81131a70a4)