dotnet/wpf · error · ArgumentException

SR.MustBeOfType

Error message

SR.MustBeOfType

What it means

ComponentResourceKeyConverter.ConvertTo validates that the incoming value is a ComponentResourceKey; if it is not (or is null), it throws ArgumentException with the MustBeOfType message. Type converters are invoked by the XAML/designer/type conversion infrastructure, so a wrong-typed value surfaces here.

Solutions

  1. Ensure the value passed to ConvertTo is a ComponentResourceKey instance
  2. Null-check the value before invoking the converter
  3. Verify the property's declared type matches what you assign

Example fix

// before
converter.ConvertTo(null, culture, someObject, typeof(string)); // throws if someObject isn't a ComponentResourceKey
// after
if (someObject is ComponentResourceKey key)
    converter.ConvertTo(null, culture, key, typeof(string));
Defensive patterns

Strategy: type-guard

Validate before calling

if (value is not ComponentResourceKey) throw new InvalidOperationException("ConvertTo requires a ComponentResourceKey");

Type guard

bool IsComponentResourceKey(object v) => v is ComponentResourceKey;

Try / catch

try { result = converter.ConvertTo(ctx, culture, value, typeof(string)); } catch (ArgumentException ex) when (ex.ParamName == "value") { /* wrong type */ }

Prevention

When it happens

Trigger: Calling ConvertTo with a value that is not a ComponentResourceKey (or null), typically via TypeConverter infrastructure or ConvertFromString/ConvertTo pipelines.

Common situations: XAML markup extension or designer passing the wrong object into the converter; reflection-driven serialization of a property whose runtime type differs from the declared key type.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ComponentResourceKeyConverter.cs:100

        /// <param name="culture">
        ///     current culture (see CLR specs)
        /// </param>
        /// <param name="value">
        ///     value to convert from
        /// </param>
        /// <param name="destinationType">
        ///     Type to convert to
        /// </param>
        /// <returns>
        ///     converted value
        /// </returns>
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            // Validate Input Arguments
            ComponentResourceKey key = value as ComponentResourceKey;
            if (key == null)
            {
                throw new ArgumentException(SR.Format(SR.MustBeOfType, "value", "ComponentResourceKey")); 
            }
            ArgumentNullException.ThrowIfNull(destinationType);
            return base.CanConvertTo(context, destinationType);
        }
    }
}

View on GitHub (pinned to 81131a70a4)