dotnet/wpf · error · ArgumentException

SR.Format(SR.General_Expected_Type, "Transform")

Error message

SR.Format(SR.General_Expected_Type, "Transform")

What it means

TransformConverter.CanConvertTo only supports conversion to string when the TypeDescriptor context's Instance is actually a Transform. If the serialization engine supplies a context whose instance is some other object, the converter throws ArgumentException(SR.General_Expected_Type, "Transform") naming the context parameter, because it cannot reason about a non-Transform instance.

Solutions

  1. Ensure the ITypeDescriptorContext passed to the converter has an Instance that is a Transform (or derived type)
  2. Pass a null context instead of one with a non-Transform Instance when calling the converter outside the WPF serialization engine
  3. Verify the object you are serializing is actually a Transform before routing it through TransformConverter

Example fix

// before
converter.CanConvertTo(new DescriptorContext { Instance = someObject }, typeof(string));
// after
if (someObject is Transform)
    converter.CanConvertTo(new DescriptorContext { Instance = someObject }, typeof(string));
Defensive patterns

Strategy: type-guard

Validate before calling

if (context?.Instance is not Transform)
    throw new ArgumentException("context.Instance must be a Transform", nameof(context));

Type guard

static bool IsValidConverterContext(ITypeDescriptorContext ctx) => ctx?.Instance is Transform;

Try / catch

try { ok = converter.CanConvertTo(context, typeof(string)); }
catch (ArgumentException ex) { /* context.Instance was not a Transform; pass null context or correct instance */ }

Prevention

When it happens

Trigger: Invoking the type converter through the serialization/deserialization engine with an ITypeDescriptorContext whose Instance property holds an object that is not a Transform (e.g. a TransformGroup or unrelated object) while querying CanConvertTo(typeof(string)) or attempting string conversion.

Common situations: Custom designer or serialization pipelines that pass the wrong instance as context.Instance; reusing the converter manually with a null-ish or wrong-typed context; version mismatches where the surrounding object model changed.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/TransformConverter.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 Transform))
                    {
                        throw new ArgumentException(SR.Format(SR.General_Expected_Type, "Transform"), nameof(context));
                    }

                    Transform value = (Transform)context.Instance;

                    return value.CanSerializeToString();
                }

                return true;
            }

            return base.CanConvertTo(context, destinationType);
        }

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

View on GitHub (pinned to 81131a70a4)