{"record":{"id":"fb4c94767c687a6d","repo":"unoplatform/uno","slug":"cannot-cast-object-0-to-string","errorCode":null,"errorMessage":"Cannot cast object '{0}' to string","messagePattern":"Cannot cast object '(.+?)' to string","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/SourceGenerators/System.Xaml/System.Xaml/TypeExtensionMethods.cs","lineNumber":124,"sourceCode":"\t\tpublic static string GetStringValue (XamlType xt, XamlMember xm, object obj, IValueSerializerContext vsctx)\n\t\t{\n\t\t\tif (obj == null)\n\t\t\t\treturn String.Empty;\n\t\t\tif (obj is Type)\n\t\t\t\treturn new XamlTypeName (xt.SchemaContext.GetXamlType ((Type) obj)).ToString (vsctx != null ? vsctx.GetService (typeof (INamespacePrefixLookup)) as INamespacePrefixLookup : null);\n\n\t\t\tvar vs = (xm != null ? xm.ValueSerializer : null) ?? xt.ValueSerializer;\n\t\t\tif (vs != null)\n\t\t\t\treturn vs.ConverterInstance.ConvertToString (obj, vsctx);\n\n\t\t\t// FIXME: does this make sense?\n\t\t\tvar vc = (xm != null ? xm.TypeConverter : null) ?? xt.TypeConverter;\n\t\t\tvar tc = vc != null ? vc.ConverterInstance : null;\n\t\t\tif (tc != null && typeof (string) != null && tc.CanConvertTo (vsctx, typeof (string)))\n\t\t\t\treturn (string) tc.ConvertTo (vsctx, CultureInfo.InvariantCulture, obj, typeof (string));\n\t\t\tif (obj is string || obj == null)\n\t\t\t\treturn (string) obj;\n\t\t\tthrow new InvalidCastException (String.Format (CultureInfo.InvariantCulture, \"Cannot cast object '{0}' to string\", obj.GetType ()));\n\t\t}\n\t\t\n\t\tpublic static TypeConverter GetTypeConverter (this Type type)\n\t\t{\n\t\t\treturn TypeDescriptor.GetConverter (type);\n\t\t}\n\t\t\n\t\t// FIXME: I want this to cover all the existing types and make it valid in both MOBILE and !MOBILE.\n\t\tclass ConvertibleTypeConverter<T> : TypeConverter\n\t\t{\n\t\t\tType type;\n\t\t\tpublic ConvertibleTypeConverter ()\n\t\t\t{\n\t\t\t\tthis.type = typeof (T);\n\t\t\t}\n\t\t\tpublic override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)\n\t\t\t{\n\t\t\t\treturn sourceType == typeof (string);","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/SourceGenerators/System.Xaml/System.Xaml/TypeExtensionMethods.cs#L106-L142","documentation":"Thrown by TypeExtensionMethods.ConvertToString when no ValueSerializer or TypeConverter can convert the object to string and the object is not already a string (or null). This is the fallback path after all conversion strategies (ValueSerializer, TypeConverter.CanConvertTo) have been exhausted; it indicates the object's type has no registered string serialization.","triggerScenarios":"Calling the internal ConvertToString extension on an object whose type has neither a ValueSerializer nor a TypeConverter that supports ConvertTo(string). Common for complex objects, anonymous types, or types without TypeConverter attributes.","commonSituations":"XAML serialization of an object graph that contains a non-serializable property value. A custom XamlType or XamlMember whose Type lacks appropriate value-serializer/type-converter metadata. Version mismatches where a converter was removed or renamed.","solutions":["Register a TypeConverter or ValueSerializer on the object's type (via [TypeConverter] or [ValueSerializer] attribute) that supports string conversion.","Avoid passing objects of types without XAML serialization support into XAML readers/writers.","If you control the type, implement IConvertible or provide a TypeConverter that handles ConvertTo(string)."],"exampleFix":"// before\npublic class MyData { /* no converter */ }\nstring s = myDataObject.ConvertToString(ctx); // throws\n\n// after\n[TypeConverter(typeof(MyDataTypeConverter))]\npublic class MyData { }\n// MyDataTypeConverter overrides ConvertTo to produce a string","handlingStrategy":"try-catch","validationCode":"var tc = TypeDescriptor.GetConverter(obj.GetType());\nif (tc != null && tc.CanConvertTo(typeof(string)))\n    return (string)tc.ConvertTo(obj, typeof(string));\nthrow new InvalidOperationException($\"No string converter for {obj.GetType()}\");","typeGuard":"static bool HasStringConverter(Type t)\n    => TypeDescriptor.GetConverter(t)?.CanConvertTo(typeof(string)) == true;","tryCatchPattern":"try { return obj.ConvertToString(ctx); }\ncatch (InvalidCastException) { return obj?.ToString(); }","preventionTips":["Register [TypeConverter] or [ValueSerializer] attributes on types that participate in XAML.","Check for a string-capable TypeConverter before attempting XAML serialization of an object."],"tags":["xaml","serialization","type-converter","invalid-cast","uno-xaml"],"backgroundTag":null,"analyzedSha":"04183404888ea21b4e5bfa3493e8d5f15e972c3a","analyzedAt":"2026-08-13T21:08:11.651Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}