{"record":{"id":"8b9ce00486586dc7","repo":"dotnet/wpf","slug":"sr-format-sr-general-badtype-convertfrom-8b9ce0","errorCode":null,"errorMessage":"SR.Format(SR.General_BadType, \"ConvertFrom\")","messagePattern":"SR\\.Format\\(SR\\.General_BadType, \"ConvertFrom\"\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/RequestCachePolicyConverter.cs","lineNumber":72,"sourceCode":"        /// <summary>\n        /// ConvertFrom - attempt to convert to a RequestCachePolicy from the given object\n        /// </summary>\n        /// <exception cref=\"NotSupportedException\">\n        /// A NotSupportedException is thrown if the example object is null or is not a valid type\n        /// which can be converted to a RequestCachePolicy.\n        /// </exception>\n        public override object ConvertFrom(ITypeDescriptorContext td, System.Globalization.CultureInfo ci, object value)\n        {\n            if (null == value)\n            {\n                throw GetConvertFromException(value);\n            }\n\n            string s = value as string;\n\n            if (null == s)\n            {\n                throw new ArgumentException(SR.Format(SR.General_BadType, \"ConvertFrom\"), nameof(value));\n            }\n\n            HttpRequestCacheLevel level = Enum.Parse<HttpRequestCacheLevel>(s, true);\n            \n            return new HttpRequestCachePolicy(level);\n        }\n\n\n        /// <summary>\n        /// ConvertTo - Attempt to convert to the given type\n        /// </summary>\n        /// <returns>\n        /// The object which was constructed.\n        /// </returns>\n        /// <exception cref=\"ArgumentNullException\">\n        /// An ArgumentNullException is thrown if the example object is null.\n        /// </exception>\n        /// <exception cref=\"ArgumentException\">","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/RequestCachePolicyConverter.cs#L54-L90","documentation":"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.","triggerScenarios":"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.","commonSituations":"Custom code feeding typed values into the converter instead of strings; XAML source generators passing unexpected value types; round-tripping HttpRequestCachePolicy through converters manually.","solutions":["Pass a string containing a valid HttpRequestCacheLevel name (e.g. \"Default\", \"CacheIfAvailable\", \"NoCacheNoDoNotStore\")","Convert enums to their string name before calling ConvertFrom, or construct HttpRequestCachePolicy directly","Guard with a type check before invoking the converter","Use ConvertTo/ConvertFrom symmetrically: ConvertFrom for strings, ConvertTo for producing strings/InstanceDescriptor"],"exampleFix":"// before\nconverter.ConvertFrom(ctx, culture, HttpRequestCacheLevel.Default); // throws\n// after\nvar policy = (HttpRequestCachePolicy)converter.ConvertFrom(ctx, culture, HttpRequestCacheLevel.Default.ToString());","handlingStrategy":"type-guard","validationCode":"if (value is not string s) throw new ArgumentException(\"ConvertFrom requires a string HttpRequestCacheLevel name\", nameof(value));\nEnum.TryParse<HttpRequestCacheLevel>(s, true, out var _);","typeGuard":"bool IsCacheLevelString(object v) => v is string s && Enum.GetNames<HttpRequestCacheLevel>().Any(n => n.Equals(s, StringComparison.OrdinalIgnoreCase));","tryCatchPattern":"try { return (HttpRequestCachePolicy)converter.ConvertFrom(ctx, culture, value); }\ncatch (ArgumentException ex) { log(ex); return new HttpRequestCachePolicy(HttpRequestCacheLevel.Default); }","preventionTips":["Always pass strings into ConvertFrom, typed values directly to constructors","Validate level names against HttpRequestCacheLevel before conversion","Prefer constructing HttpRequestCachePolicy directly outside XAML","Keep ConvertFrom/ConvertTo usage symmetrical in serialization code"],"tags":["wpf","typeconverter","cache-policy","argument"],"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"}