unoplatform/uno · error · InvalidCastException

Cannot cast object '{0}' to string

Error message

Cannot cast object '{0}' to string

What it means

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.

Source

Thrown at src/SourceGenerators/System.Xaml/System.Xaml/TypeExtensionMethods.cs:124

		public static string GetStringValue (XamlType xt, XamlMember xm, object obj, IValueSerializerContext vsctx)
		{
			if (obj == null)
				return String.Empty;
			if (obj is Type)
				return new XamlTypeName (xt.SchemaContext.GetXamlType ((Type) obj)).ToString (vsctx != null ? vsctx.GetService (typeof (INamespacePrefixLookup)) as INamespacePrefixLookup : null);

			var vs = (xm != null ? xm.ValueSerializer : null) ?? xt.ValueSerializer;
			if (vs != null)
				return vs.ConverterInstance.ConvertToString (obj, vsctx);

			// FIXME: does this make sense?
			var vc = (xm != null ? xm.TypeConverter : null) ?? xt.TypeConverter;
			var tc = vc != null ? vc.ConverterInstance : null;
			if (tc != null && typeof (string) != null && tc.CanConvertTo (vsctx, typeof (string)))
				return (string) tc.ConvertTo (vsctx, CultureInfo.InvariantCulture, obj, typeof (string));
			if (obj is string || obj == null)
				return (string) obj;
			throw new InvalidCastException (String.Format (CultureInfo.InvariantCulture, "Cannot cast object '{0}' to string", obj.GetType ()));
		}
		
		public static TypeConverter GetTypeConverter (this Type type)
		{
			return TypeDescriptor.GetConverter (type);
		}
		
		// FIXME: I want this to cover all the existing types and make it valid in both MOBILE and !MOBILE.
		class ConvertibleTypeConverter<T> : TypeConverter
		{
			Type type;
			public ConvertibleTypeConverter ()
			{
				this.type = typeof (T);
			}
			public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
			{
				return sourceType == typeof (string);

View on GitHub (pinned to 0418340488)

Solutions

  1. Register a TypeConverter or ValueSerializer on the object's type (via [TypeConverter] or [ValueSerializer] attribute) that supports string conversion.
  2. Avoid passing objects of types without XAML serialization support into XAML readers/writers.
  3. If you control the type, implement IConvertible or provide a TypeConverter that handles ConvertTo(string).

Example fix

// before
public class MyData { /* no converter */ }
string s = myDataObject.ConvertToString(ctx); // throws

// after
[TypeConverter(typeof(MyDataTypeConverter))]
public class MyData { }
// MyDataTypeConverter overrides ConvertTo to produce a string
Defensive patterns

Strategy: try-catch

Validate before calling

var tc = TypeDescriptor.GetConverter(obj.GetType());
if (tc != null && tc.CanConvertTo(typeof(string)))
    return (string)tc.ConvertTo(obj, typeof(string));
throw new InvalidOperationException($"No string converter for {obj.GetType()}");

Type guard

static bool HasStringConverter(Type t)
    => TypeDescriptor.GetConverter(t)?.CanConvertTo(typeof(string)) == true;

Try / catch

try { return obj.ConvertToString(ctx); }
catch (InvalidCastException) { return obj?.ToString(); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13). Data as JSON: /api/errors/fb4c94767c687a6d. Report an issue: GitHub.