{"record":{"id":"91d8be833c3c9c37","repo":"unoplatform/uno","slug":"could-not-convert-object-0-of-type-1-to-2","errorCode":null,"errorMessage":"Could not convert object '{0}' (of type {1}) to {2}: ","messagePattern":"Could not convert object '(.+?)' \\(of type (.+?)\\) to (.+?): ","errorType":"exception","errorClass":"XamlObjectWriterException","httpStatus":null,"severity":"error","filePath":"src/SourceGenerators/System.Xaml/System.Xaml/XamlObjectWriter.cs","lineNumber":500,"sourceCode":"\t\t\t\tif (xt.IsDictionary)\n\t\t\t\t\tmt.Invoker.AddToDictionary (parent, GetCorrectlyTypedValue (null, xt.KeyType, keyObj), GetCorrectlyTypedValue (null, xt.ItemType, obj));\n\t\t\t\telse // collection. Note that state.Type isn't usable for PositionalParameters to identify collection kind.\n\t\t\t\t\tmt.Invoker.AddToCollection (parent, GetCorrectlyTypedValue (null, xt.ItemType, obj));\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\telse\n\t\t\t\treturn false;\n\t\t}\n\n\t\tobject GetCorrectlyTypedValue (XamlMember xm, XamlType xt, object value)\n\t\t{\n\t\t\ttry {\n\t\t\t\treturn DoGetCorrectlyTypedValue (xm, xt, value);\n\t\t\t} catch (XamlObjectWriterException) {\n\t\t\t\tthrow;\n\t\t\t} catch (Exception ex) {\n\t\t\t\t// For + ex.Message, the runtime should print InnerException message like .NET does.\n\t\t\t\tthrow new XamlObjectWriterException (String.Format (CultureInfo.InvariantCulture, \"Could not convert object \\'{0}' (of type {1}) to {2}: \", value, value != null ? (object) value.GetType () : \"(null)\", xt)  + ex.Message, ex);\n\t\t\t}\n\t\t}\n\n\t\t// It expects that it is not invoked when there is no value to \n\t\t// assign.\n\t\t// When it is passed null, then it returns a default instance.\n\t\t// For example, passing null as Int32 results in 0.\n\t\t// But do not immediately try to instantiate with the type, since the type might be abstract.\n\t\tobject DoGetCorrectlyTypedValue (XamlMember xm, XamlType xt, object value)\n\t\t{\n\t\t\tif (value == null) {\n\t\t\t\tif (xt.IsContentValue (service_provider)) // it is for collection/dictionary key and item\n\t\t\t\t\treturn null;\n\t\t\t\telse\n\t\t\t\t\treturn xt.IsNullable ? null : xt.Invoker.CreateInstance (Array.Empty<object>());\n\t\t\t}\n\t\t\tif (xt == null)\n\t\t\t\treturn value;","sourceCodeStart":482,"sourceCodeEnd":518,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/SourceGenerators/System.Xaml/System.Xaml/XamlObjectWriter.cs#L482-L518","documentation":"A wrapper thrown by GetCorrectlyTypedValue when coercing a value to the target type/member throws any exception that is not already a XamlObjectWriterException. The inner exception holds the real conversion error, and its message is appended after the colon. The text 'Could not convert object ...' is the generic framing.","triggerScenarios":"A registered TypeConverter throws during ConvertFrom — e.g. Int32Converter receiving 'abc', a custom converter rejecting input, or a culture-sensitive parse failing.","commonSituations":"Malformed string values in XAML attributes; custom TypeConverter bugs; locale/culture parsing differences; null where a value type was expected.","solutions":["Read the InnerException message (appended after the colon) to find the real conversion failure.","Correct the source value so the type converter accepts it.","Verify the expected TypeConverter is registered for the target type and is functioning."],"exampleFix":"<!-- before -->\n<Slider Value=\"abc\" />\n<!-- after -->\n<Slider Value=\"42\" />","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { /* load/write XAML */ }\ncatch (XamlObjectWriterException ex) when (ex.Message.Contains(\"Could not convert\")) {\n    // ex.InnerException has the converter's failure; fix the source value or the TypeConverter.\n    throw new InvalidOperationException(\"Type conversion failed\", ex.InnerException);\n}","preventionTips":["Validate string values match the target type's converter before/while authoring XAML.","Unit-test custom TypeConverter.ConvertFrom with representative inputs.","Always read InnerException to find the real conversion error."],"tags":["xaml","type-conversion","inner-exception","object-writer"],"backgroundTag":null,"analyzedSha":"04183404888ea21b4e5bfa3493e8d5f15e972c3a","analyzedAt":"2026-08-13T21:08:11.651Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}