dotnet/wpf · error · ArgumentException

SR.Format(SR.MustBeOfType, "value"…

Error message

SR.Format(SR.MustBeOfType, "value", "ResourceReferenceExpression")

What it means

Thrown as ArgumentException from ResourceReferenceExpressionConverter.ConvertTo when the 'value' argument is not a ResourceReferenceExpression. The converter only knows how to convert that specific expression type to MarkupExtension or InstanceDescriptor.

Solutions

  1. Check `value is ResourceReferenceExpression` before calling ConvertTo
  2. Use the correct converter type for the expression you actually have
  3. Filter collections to only ResourceReferenceExpression items before conversion

Example fix

// before
converter.ConvertTo(null, culture, someOtherExpression, typeof(MarkupExtension));
// after
if (value is ResourceReferenceExpression expr)
    converter.ConvertTo(null, culture, expr, typeof(MarkupExtension));
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(value is ResourceReferenceExpression)) throw new ArgumentException("value must be a ResourceReferenceExpression");

Type guard

bool IsResourceRef(object v) => v is ResourceReferenceExpression;

Try / catch

try { result = converter.ConvertTo(ctx, culture, value, destType); } catch (ArgumentException) { /* value was not a ResourceReferenceExpression */ }

Prevention

When it happens

Trigger: Calling ConvertTo with a value that is null or a different Expression/MarkupExtension type; type converters invoked reflectively (e.g. designer serialization, XAML writer) over a mixed expression collection.

Common situations: Design-time tooling or XAML serialization walking properties whose value is not a resource reference; custom converters passing the wrong object.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ResourceReferenceExpressionConverter.cs:116

        /// <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
            ResourceReferenceExpression expr = value as ResourceReferenceExpression;
            if (expr == null)
            {
                throw new ArgumentException(SR.Format(SR.MustBeOfType, "value", "ResourceReferenceExpression"));
            }
            ArgumentNullException.ThrowIfNull(destinationType);

            // MarkupExtension
            if (destinationType == typeof(MarkupExtension))
            {
                return new DynamicResourceExtension(expr.ResourceKey);
            }

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

View on GitHub (pinned to 81131a70a4)