dotnet/wpf · error · ArgumentException

SR.General_Expected_Type

Error message

SR.General_Expected_Type

What it means

PathFigureCollectionConverter.CanConvertTo throws ArgumentException with SR.General_Expected_Type when the serialization context supplies an Instance that is not a PathFigureCollection. The converter only supports converting to string for actual PathFigureCollection instances.

Solutions

  1. Pass a context whose Instance is a PathFigureCollection.
  2. Check context.Instance is PathFigureCollection before invoking the converter and return false otherwise.
  3. Use the correct TypeConverter for the object type being serialized.

Example fix

// before
bool ok = converter.CanConvertTo(context, typeof(string)); // context.Instance is not a PathFigureCollection
// after
bool ok = context?.Instance is PathFigureCollection && converter.CanConvertTo(context, typeof(string));
Defensive patterns

Strategy: type-guard

Validate before calling

if (context?.Instance is PathFigureCollection)
    return converter.CanConvertTo(context, destinationType);
return false;

Type guard

context?.Instance is PathFigureCollection collection

Try / catch

try
{
    return converter.CanConvertTo(context, typeof(string));
}
catch (ArgumentException)
{
    return false; // context.Instance was not a PathFigureCollection
}

Prevention

When it happens

Trigger: Calling CanConvertTo(context, typeof(string)) with an ITypeDescriptorContext whose Instance property holds an object of a different type.

Common situations: Reusing the converter manually with a mismatched context instance; designer/serialization pipelines passing the wrong object as context.Instance; custom TypeDescriptor setups wiring this converter to another type.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/PathFigureCollectionConverter.cs:67

        /// <summary>
        /// Returns true if this type converter can convert to the given type.
        /// </summary>
        /// <returns>
        /// bool - True if this converter can convert to the provided type, false if not.
        /// </returns>
        /// <param name="context"> The ITypeDescriptorContext for this call. </param>
        /// <param name="destinationType"> The Type being queried for support. </param>
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                // When invoked by the serialization engine we can convert to string only for some instances
                if (context != null && context.Instance != null)
                {
                    if (!(context.Instance is PathFigureCollection))
                    {
                        throw new ArgumentException(SR.Format(SR.General_Expected_Type, "PathFigureCollection"), nameof(context));
                    }

                    PathFigureCollection value = (PathFigureCollection)context.Instance;

                    return value.CanSerializeToString();
                }

                return true;
            }

            return base.CanConvertTo(context, destinationType);
        }

        /// <summary>
        /// Attempts to convert to a PathFigureCollection from the given object.
        /// </summary>
        /// <returns>
        /// The PathFigureCollection which was constructed.

View on GitHub (pinned to 81131a70a4)