unoplatform/uno · error · XamlObjectWriterException
RootObjectInstance type '{0}' is not assignable to '{1}'
Error message
RootObjectInstance type '{0}' is not assignable to '{1}' What it means
Thrown when XamlObjectWriterSettings.RootObjectInstance is set and the writer begins the root object. The writer skips construction and reuses the provided instance, but only if that instance's runtime type is assignable to the root element's underlying CLR type. If the types are incompatible, this exception is thrown.
Source
Thrown at src/SourceGenerators/System.Xaml/System.Xaml/XamlObjectWriter.cs:244
public INameScope NameScope {
get { return name_scope; }
}
public object Result { get; set; }
protected override void OnWriteStartObject ()
{
var state = object_states.Pop ();
if (object_states.Count > 0) {
var pstate = object_states.Peek ();
if (CurrentMemberState.Value != null)
throw new XamlDuplicateMemberException (String.Format (CultureInfo.InvariantCulture, "Member '{0}' is already written to current type '{1}'", CurrentMember, pstate.Type));
} else {
var obj = source.Settings.RootObjectInstance;
if (obj != null) {
if (state.Type.UnderlyingType != null && !state.Type.UnderlyingType.IsAssignableFrom (obj.GetType ()))
throw new XamlObjectWriterException (String.Format (CultureInfo.InvariantCulture, "RootObjectInstance type '{0}' is not assignable to '{1}'", obj.GetType (), state.Type));
state.Value = obj;
state.IsInstantiated = true;
}
root_state = state;
}
object_states.Push (state);
if (!state.Type.IsContentValue (service_provider))
InitializeObjectIfRequired (true);
state.IsXamlWriterCreated = true;
source.OnBeforeProperties (state.Value);
}
protected override void OnWriteGetObject ()
{
var state = object_states.Pop ();
var xm = CurrentMember;
var instance = xm.Invoker.GetValue (object_states.Peek ().Value);View on GitHub (pinned to 0418340488)
Solutions
- Ensure RootObjectInstance is exactly the root type or a derived type.
- If the root type changed, update the pre-allocated instance's type to match.
- Omit RootObjectInstance when you cannot guarantee type compatibility, letting the writer construct the instance.
Example fix
// before settings.RootObjectInstance = new OldViewModel(); writer.WriteStartObject(new XamlType(typeof(NewViewModel), ctx)); // after settings.RootObjectInstance = new NewViewModel(); writer.WriteStartObject(new XamlType(typeof(NewViewModel), ctx));
Defensive patterns
Strategy: validation
Validate before calling
if (settings.RootObjectInstance != null) {
var rootType = new XamlType(targetType, schemaContext);
if (rootType.UnderlyingType != null &&
!rootType.UnderlyingType.IsAssignableFrom(settings.RootObjectInstance.GetType()))
throw new InvalidOperationException($"RootObjectInstance type mismatch: expected {rootType.UnderlyingType}");
} Try / catch
try { writer.WriteStartObject(rootXamlType); }
catch (XamlObjectWriterException ex) when (ex.Message.Contains("RootObjectInstance")) {
// reconcile the pre-allocated instance type with the XAML root type.
} Prevention
- Keep the RootObjectInstance type and the XAML root element type in lockstep; revisit both after refactors.
- Prefer letting the writer construct the root when type compatibility is uncertain.
- Add a unit test asserting RootObjectInstance.GetType() is assignable to the root type.
When it happens
Trigger: Setting `settings.RootObjectInstance = new Foo()` then writing a root element whose XamlType.UnderlyingType is `Bar` (where Foo is not a Bar).
Common situations: Instance-reuse/caching scenarios where the cached instance's type drifted from the XAML root after a refactor; deserialization frameworks that pre-allocate a default instance then change the root type.
Related errors
- Value '{0}' (of type {1}) is not of or convertible to type {
- Value must either be null or of type bool. Got {value} ({val
- Value must either be null or of type bool. Got {value} ({val
- Value must either be null or of type bool. Got {value} ({val
- Value must either be null or of type bool. Got {value} ({val
AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13).
Data as JSON: /api/errors/9e44dd1f932dc7cd.
Report an issue: GitHub.