unoplatform/uno · error · XamlObjectReaderException
instance type '{0}' has no default constructor.
Error message
instance type '{0}' has no default constructor. What it means
Thrown by the XamlObjectReader constructor when the instance type has no parameterless constructor, no XAML constructor arguments ([ConstructorArgument] / x:ConstructorArguments), and no TypeConverter. XamlObjectReader needs a way to reconstruct the object from XAML at read-back time; without a default constructor, constructor arguments, or a type converter, the type is not XAML-constructible.
Source
Thrown at src/SourceGenerators/System.Xaml/System.Xaml/XamlObjectReader.cs:99
if (instance is Type)
instance = new TypeExtension ((Type) instance);
// See also Instance property for this weirdness.
this.root_raw = instance;
instance = TypeExtensionMethods.GetExtensionWrapped (instance);
this.root = instance;
sctx = schemaContext;
// this.settings = settings;
// check type validity. Note that some checks also needs done at Read() phase. (it is likely FIXME:)
if (instance != null) {
var type = new InstanceContext (instance).GetRawValue ().GetType ();
if (!type.IsPublic)
throw new XamlObjectReaderException (String.Format (CultureInfo.InvariantCulture, "instance type '{0}' must be public and non-nested.", type));
var xt = SchemaContext.GetXamlType (type);
if (xt.ConstructionRequiresArguments && !xt.GetConstructorArguments ().Any () && xt.TypeConverter == null)
throw new XamlObjectReaderException (String.Format (CultureInfo.InvariantCulture, "instance type '{0}' has no default constructor.", type));
}
value_serializer_context = new ValueSerializerContext (new PrefixLookup (sctx), sctx, null);
new XamlObjectNodeIterator (instance, sctx, value_serializer_context).PrepareReading ();
}
bool is_eof;
object root, root_raw;
XamlSchemaContext sctx;
// XamlObjectReaderSettings settings;
IValueSerializerContext value_serializer_context;
IEnumerator<NamespaceDeclaration> ns_iterator;
IEnumerator<XamlNodeInfo> nodes;
PrefixLookup PrefixLookup {
get { return (PrefixLookup) value_serializer_context.GetService (typeof (INamespacePrefixLookup)); }
}View on GitHub (pinned to 0418340488)
Solutions
- Add a parameterless constructor to the type so XAML can instantiate it.
- Add a TypeConverter that can create instances from string (e.g. [TypeConverter(typeof(MyConverter))]).
- Supply constructor arguments via [ConstructorArgument] attributes on properties (and ensure GetConstructorArguments returns them).
- If the type is immutable by design, wrap it in a public mutable surrogate type for XAML serialization.
Example fix
// before
public class MyData
{
public MyData(string value) { Value = value; }
public string Value { get; set; }
}
var reader = new XamlObjectReader(new MyData("x"), sctx); // throws
// after
public class MyData
{
public MyData() { } // parameterless ctor added
public MyData(string value) { Value = value; }
public string Value { get; set; }
} Defensive patterns
Strategy: validation
Validate before calling
var type = instance.GetType();
if (type.GetConstructor(Type.EmptyTypes) == null
&& !HasConstructorArguments(type)
&& TypeDescriptor.GetConverter(type) == null)
throw new InvalidOperationException($"{type} is not XAML-constructible."); Type guard
static bool IsXamlConstructible(Type t)
=> t.GetConstructor(Type.EmptyTypes) != null
|| t.GetCustomAttribute<TypeConverterAttribute>() != null; Try / catch
try { var reader = new XamlObjectReader(obj, sctx); }
catch (XamlObjectReaderException) { /* add ctor or TypeConverter */ } Prevention
- Ensure serializable types have a parameterless constructor or a TypeConverter.
- Add [TypeConverter] for immutable types that cannot add a default constructor.
When it happens
Trigger: Constructing new XamlObjectReader(instance, sctx) where the type's only constructors require parameters and the type has neither [TypeConverter] nor [ConstructorArgument] metadata.
Common situations: Serializing DTOs or domain objects that only have parameterized constructors (e.g. record types, immutable classes). Types designed for dependency injection with required constructor parameters but no XAML markup support.
Related errors
- Conversion from type {0} is not supported
- Conversion to type {0} is not supported
- Cannot cast object '{0}' to string
- There is already an object of type {0} named as '{1}'. Objec
- schemaContext
AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13).
Data as JSON: /api/errors/2f003cdcb79b00bd.
Report an issue: GitHub.