dotnet/wpf · error · ArgumentException

SR.General_Expected_Type (CacheMode)

Error message

SR.General_Expected_Type (CacheMode)

What it means

CacheModeConverter.CanConvertTo throws ArgumentException when the context's Instance is not a CacheMode. The converter only knows how to judge string-serializability of CacheMode instances (e.g. BitmapCache), so it validates the instance type with SR.General_Expected_Type before casting.

Solutions

  1. Set context.Instance to the actual CacheMode-derived object (e.g. BitmapCache) before calling CanConvertTo.
  2. Pass a null context or null Instance to bypass the instance check.
  3. Fix the context-populating caller to bind the object being converted.
  4. Use the value's own TypeConverter when the value is not a CacheMode.

Example fix

// before
ctx.Instance = visual; // wrong object
converter.CanConvertTo(ctx, typeof(string));
// after
ctx.Instance = new BitmapCache(); // CacheMode instance
converter.CanConvertTo(ctx, typeof(string));
Defensive patterns

Strategy: type-guard

Validate before calling

if (ctx != null && ctx.Instance != null && !(ctx.Instance is CacheMode)) throw new ArgumentException("Instance must be a CacheMode");

Type guard

bool canQuery = ctx == null || ctx.Instance == null || ctx.Instance is CacheMode;

Try / catch

try { return converter.CanConvertTo(ctx, typeof(string)); } catch (ArgumentException) { return false; }

Prevention

When it happens

Trigger: Calling CanConvertTo(ITypeDescriptorContext, Type) (to string) with a context whose Instance is not a CacheMode — e.g. a Brush, UIElement, or arbitrary object placed in the serialization context.

Common situations: Serialization pipelines reusing a generic type descriptor context across properties and forgetting to swap Instance; converting UIElement.CacheMode with the wrong object bound to the context.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/c8573dbe9c13eb65. Report an issue: GitHub.

Appendix: source

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

                    CacheMode value = (CacheMode)context.Instance;

                    return value.CanSerializeToString();
                }

                return true;
            }

            return base.CanConvertTo(context, destinationType);
        }

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

View on GitHub (pinned to 81131a70a4)