{"record":{"id":"bb3f35ecde94b63d","repo":"dotnet/wpf","slug":"sr-format-sr-general-expected-type-transform","errorCode":null,"errorMessage":"SR.Format(SR.General_Expected_Type, \"Transform\")","messagePattern":"SR\\.Format\\(SR\\.General_Expected_Type, \"Transform\"\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/TransformConverter.cs","lineNumber":67,"sourceCode":"\n        /// <summary>\n        /// Returns true if this type converter can convert to the given type.\n        /// </summary>\n        /// <returns>\n        /// bool - True if this converter can convert to the provided type, false if not.\n        /// </returns>\n        /// <param name=\"context\"> The ITypeDescriptorContext for this call. </param>\n        /// <param name=\"destinationType\"> The Type being queried for support. </param>\n        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)\n        {\n            if (destinationType == typeof(string))\n            {\n                // When invoked by the serialization engine we can convert to string only for some instances\n                if (context != null && context.Instance != null)\n                {\n                    if (!(context.Instance is Transform))\n                    {\n                        throw new ArgumentException(SR.Format(SR.General_Expected_Type, \"Transform\"), nameof(context));\n                    }\n\n                    Transform value = (Transform)context.Instance;\n\n                    return value.CanSerializeToString();\n                }\n\n                return true;\n            }\n\n            return base.CanConvertTo(context, destinationType);\n        }\n\n        /// <summary>\n        /// Attempts to convert to a Transform from the given object.\n        /// </summary>\n        /// <returns>\n        /// The Transform which was constructed.","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/TransformConverter.cs#L49-L85","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the ITypeDescriptorContext passed to the converter has an Instance that is a Transform (or derived type)","Pass a null context instead of one with a non-Transform Instance when calling the converter outside the WPF serialization engine","Verify the object you are serializing is actually a Transform before routing it through TransformConverter"],"exampleFix":"// before\nconverter.CanConvertTo(new DescriptorContext { Instance = someObject }, typeof(string));\n// after\nif (someObject is Transform)\n    converter.CanConvertTo(new DescriptorContext { Instance = someObject }, typeof(string));","handlingStrategy":"type-guard","validationCode":"if (context?.Instance is not Transform)\n    throw new ArgumentException(\"context.Instance must be a Transform\", nameof(context));","typeGuard":"static bool IsValidConverterContext(ITypeDescriptorContext ctx) => ctx?.Instance is Transform;","tryCatchPattern":"try { ok = converter.CanConvertTo(context, typeof(string)); }\ncatch (ArgumentException ex) { /* context.Instance was not a Transform; pass null context or correct instance */ }","preventionTips":["Always supply an ITypeDescriptorContext whose Instance is the Transform being serialized","Pass null context when invoking the converter outside the WPF serialization engine","Assert the instance type before routing through TransformConverter"],"tags":["wpf","type-converter","serialization","argument-exception"],"backgroundTag":"type-mismatch","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}