dotnet/wpf · error · ArgumentException

SR.General_Expected_Type

Error message

SR.General_Expected_Type

What it means

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.

Solutions

  1. Ensure the ITypeDescriptorContext passed to the converter has an Instance of the exact generated struct type
  2. Register the correct TypeConverter for the type in question
  3. If creating the context yourself, set context.Instance to an instance of the resource struct before conversion
  4. Catch ArgumentException and validate the instance type before calling the converter

Example fix

// before
converter.ConvertToString(new Context(wrongObj), null, value); // throws
// after
if (value is MyStruct)
    converter.ConvertToString(new Context(value), null, value);
Defensive patterns

Strategy: type-guard

Validate before calling

if (context?.Instance is MyStruct) { /* safe to convert */ }

Type guard

bool IsExpectedInstance(ITypeDescriptorContext ctx) => ctx?.Instance is MyStruct;

Try / catch

try { return converter.ConvertToString(ctx, culture, value); }
catch (ArgumentException) { throw new InvalidOperationException("Converter context instance type mismatch"); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WpfGfx/codegen/mcg/generators/ManagedStruct.cs:726

                    {
                        csFile.Write(
                            [[inline]]
                                using [[s]];
                            [[/inline]]
                                );
                    }

                    // If the resource may not always be serializable as a string, we need to check if this instance can.
                    if (!resource.IsAlwaysSerializableAsString)
                    {
                        serializationContextCanConvertTo =
                            [[inline]]
                                // 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 [[resource.Name]]))
                                    {
                                        throw new ArgumentException(SR.Format(SR.General_Expected_Type, "[[resource.Name]]"), nameof(context));
                                    }

                                    [[resource.Name]] value = ([[resource.Name]])context.Instance;

                                    return value.CanSerializeToString();
                                }


                            [[/inline]]
                                ;

                        serializationContextConvertTo =
                            [[inline]]
                                // When invoked by the serialization engine we can convert to string only for some instances
                                if (context != null && context.Instance != null)
                                {
                                    if (!instance.CanSerializeToString())
                                    {

View on GitHub (pinned to 81131a70a4)