{"record":{"id":"6c5a99066497939b","repo":"dotnet/wpf","slug":"sr-general-expected-type-managedstruct","errorCode":null,"errorMessage":"SR.General_Expected_Type","messagePattern":"SR\\.General_Expected_Type","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WpfGfx/codegen/mcg/generators/ManagedStruct.cs","lineNumber":726,"sourceCode":"                    {\n                        csFile.Write(\n                            [[inline]]\n                                using [[s]];\n                            [[/inline]]\n                                );\n                    }\n\n                    // If the resource may not always be serializable as a string, we need to check if this instance can.\n                    if (!resource.IsAlwaysSerializableAsString)\n                    {\n                        serializationContextCanConvertTo =\n                            [[inline]]\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 [[resource.Name]]))\n                                    {\n                                        throw new ArgumentException(SR.Format(SR.General_Expected_Type, \"[[resource.Name]]\"), nameof(context));\n                                    }\n\n                                    [[resource.Name]] value = ([[resource.Name]])context.Instance;\n\n                                    return value.CanSerializeToString();\n                                }\n\n\n                            [[/inline]]\n                                ;\n\n                        serializationContextConvertTo =\n                            [[inline]]\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 (!instance.CanSerializeToString())\n                                    {","sourceCodeStart":708,"sourceCodeEnd":744,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WpfGfx/codegen/mcg/generators/ManagedStruct.cs#L708-L744","documentation":"This SR resource backs an ArgumentException thrown in generated TypeConverter string-conversion code (ManagedStruct.cs template) when the serialization context's Instance is not of the expected generated struct type. The template asserts context.Instance is assignable to the resource struct type before calling CanSerializeToString. It is a type-safety guard in the struct-to-string conversion path.","triggerScenarios":"Invoking the generated converter's string-conversion path with an ITypeDescriptorContext whose Instance property holds an object of a different type than the generated struct type.","commonSituations":"Wiring the wrong TypeConverter to a property descriptor or serializer; a serializer context carrying a parent/foreign object; refactoring a struct type so the registered converter no longer matches the instance type.","solutions":["Ensure the ITypeDescriptorContext passed to the converter has an Instance of the exact generated struct type","Register the correct TypeConverter for the type in question","If creating the context yourself, set context.Instance to an instance of the resource struct before conversion","Catch ArgumentException and validate the instance type before calling the converter"],"exampleFix":"// before\nconverter.ConvertToString(new Context(wrongObj), null, value); // throws\n// after\nif (value is MyStruct)\n    converter.ConvertToString(new Context(value), null, value);","handlingStrategy":"type-guard","validationCode":"if (context?.Instance is MyStruct) { /* safe to convert */ }","typeGuard":"bool IsExpectedInstance(ITypeDescriptorContext ctx) => ctx?.Instance is MyStruct;","tryCatchPattern":"try { return converter.ConvertToString(ctx, culture, value); }\ncatch (ArgumentException) { throw new InvalidOperationException(\"Converter context instance type mismatch\"); }","preventionTips":["Register one converter per exact type","Assert context.Instance type in custom serialization code before invoking converters"],"tags":["wpf","typeconverter","argumentexception","type-mismatch"],"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"}