dotnet/wpf · error · ArgumentException

SR.Format(SR.General_BadType, "ConvertFrom")

Error message

SR.Format(SR.General_BadType, "ConvertFrom")

What it means

RequestCachePolicyConverter.ConvertFrom converts a string like an HttpRequestCacheLevel name into an HttpRequestCachePolicy. If the incoming value is not a string (or null), it throws ArgumentException with General_BadType "ConvertFrom" naming parameter 'value'. The converter exists mainly for XAML/designer support of cache policies in services.

Solutions

  1. Pass a string containing a valid HttpRequestCacheLevel name (e.g. "Default", "CacheIfAvailable", "NoCacheNoDoNotStore")
  2. Convert enums to their string name before calling ConvertFrom, or construct HttpRequestCachePolicy directly
  3. Guard with a type check before invoking the converter
  4. Use ConvertTo/ConvertFrom symmetrically: ConvertFrom for strings, ConvertTo for producing strings/InstanceDescriptor

Example fix

// before
converter.ConvertFrom(ctx, culture, HttpRequestCacheLevel.Default); // throws
// after
var policy = (HttpRequestCachePolicy)converter.ConvertFrom(ctx, culture, HttpRequestCacheLevel.Default.ToString());
Defensive patterns

Strategy: type-guard

Validate before calling

if (value is not string s) throw new ArgumentException("ConvertFrom requires a string HttpRequestCacheLevel name", nameof(value));
Enum.TryParse<HttpRequestCacheLevel>(s, true, out var _);

Type guard

bool IsCacheLevelString(object v) => v is string s && Enum.GetNames<HttpRequestCacheLevel>().Any(n => n.Equals(s, StringComparison.OrdinalIgnoreCase));

Try / catch

try { return (HttpRequestCachePolicy)converter.ConvertFrom(ctx, culture, value); }
catch (ArgumentException ex) { log(ex); return new HttpRequestCachePolicy(HttpRequestCacheLevel.Default); }

Prevention

When it happens

Trigger: Calling RequestCachePolicyConverter.ConvertFrom with a non-string value — e.g. an HttpRequestCacheLevel enum instance, an int, or null — typically from XAML parsing or designer serialization pipelines.

Common situations: Custom code feeding typed values into the converter instead of strings; XAML source generators passing unexpected value types; round-tripping HttpRequestCachePolicy through converters manually.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/RequestCachePolicyConverter.cs:72

        /// <summary>
        /// ConvertFrom - attempt to convert to a RequestCachePolicy from the given object
        /// </summary>
        /// <exception cref="NotSupportedException">
        /// A NotSupportedException is thrown if the example object is null or is not a valid type
        /// which can be converted to a RequestCachePolicy.
        /// </exception>
        public override object ConvertFrom(ITypeDescriptorContext td, System.Globalization.CultureInfo ci, object value)
        {
            if (null == value)
            {
                throw GetConvertFromException(value);
            }

            string s = value as string;

            if (null == s)
            {
                throw new ArgumentException(SR.Format(SR.General_BadType, "ConvertFrom"), nameof(value));
            }

            HttpRequestCacheLevel level = Enum.Parse<HttpRequestCacheLevel>(s, true);
            
            return new HttpRequestCachePolicy(level);
        }


        /// <summary>
        /// ConvertTo - Attempt to convert to the given type
        /// </summary>
        /// <returns>
        /// The object which was constructed.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// An ArgumentNullException is thrown if the example object is null.
        /// </exception>
        /// <exception cref="ArgumentException">

View on GitHub (pinned to 81131a70a4)