{"record":{"id":"9e44dd1f932dc7cd","repo":"unoplatform/uno","slug":"rootobjectinstance-type-0-is-not-assignable-to","errorCode":null,"errorMessage":"RootObjectInstance type '{0}' is not assignable to '{1}'","messagePattern":"RootObjectInstance type '(.+?)' is not assignable to '(.+?)'","errorType":"exception","errorClass":"XamlObjectWriterException","httpStatus":null,"severity":"error","filePath":"src/SourceGenerators/System.Xaml/System.Xaml/XamlObjectWriter.cs","lineNumber":244,"sourceCode":"\n\t\tpublic INameScope NameScope {\n\t\t\tget { return name_scope; }\n\t\t}\n\n\t\tpublic object Result { get; set; }\n\t\t\n\t\tprotected override void OnWriteStartObject ()\n\t\t{\n\t\t\tvar state = object_states.Pop ();\n\t\t\tif (object_states.Count > 0) {\n\t\t\t\tvar pstate = object_states.Peek ();\n\t\t\t\tif (CurrentMemberState.Value != null)\n\t\t\t\t\tthrow new XamlDuplicateMemberException (String.Format (CultureInfo.InvariantCulture, \"Member '{0}' is already written to current type '{1}'\", CurrentMember, pstate.Type));\n\t\t\t} else {\n\t\t\t\tvar obj = source.Settings.RootObjectInstance;\n\t\t\t\tif (obj != null) {\n\t\t\t\t\tif (state.Type.UnderlyingType != null && !state.Type.UnderlyingType.IsAssignableFrom (obj.GetType ()))\n\t\t\t\t\t\tthrow new XamlObjectWriterException (String.Format (CultureInfo.InvariantCulture, \"RootObjectInstance type '{0}' is not assignable to '{1}'\", obj.GetType (), state.Type));\n\t\t\t\t\tstate.Value = obj;\n\t\t\t\t\tstate.IsInstantiated = true;\n\t\t\t\t}\n\t\t\t\troot_state = state;\n\t\t\t}\n\t\t\tobject_states.Push (state);\n\t\t\tif (!state.Type.IsContentValue (service_provider))\n\t\t\t\tInitializeObjectIfRequired (true);\n\n\t\t\tstate.IsXamlWriterCreated = true;\n\t\t\tsource.OnBeforeProperties (state.Value);\n\t\t}\n\n\t\tprotected override void OnWriteGetObject ()\n\t\t{\n\t\t\tvar state = object_states.Pop ();\n\t\t\tvar xm = CurrentMember;\n\t\t\tvar instance = xm.Invoker.GetValue (object_states.Peek ().Value);","sourceCodeStart":226,"sourceCodeEnd":262,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/SourceGenerators/System.Xaml/System.Xaml/XamlObjectWriter.cs#L226-L262","documentation":"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.","triggerScenarios":"Setting `settings.RootObjectInstance = new Foo()` then writing a root element whose XamlType.UnderlyingType is `Bar` (where Foo is not a Bar).","commonSituations":"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.","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."],"exampleFix":"// before\nsettings.RootObjectInstance = new OldViewModel();\nwriter.WriteStartObject(new XamlType(typeof(NewViewModel), ctx));\n// after\nsettings.RootObjectInstance = new NewViewModel();\nwriter.WriteStartObject(new XamlType(typeof(NewViewModel), ctx));","handlingStrategy":"validation","validationCode":"if (settings.RootObjectInstance != null) {\n    var rootType = new XamlType(targetType, schemaContext);\n    if (rootType.UnderlyingType != null &&\n        !rootType.UnderlyingType.IsAssignableFrom(settings.RootObjectInstance.GetType()))\n        throw new InvalidOperationException($\"RootObjectInstance type mismatch: expected {rootType.UnderlyingType}\");\n}","typeGuard":null,"tryCatchPattern":"try { writer.WriteStartObject(rootXamlType); }\ncatch (XamlObjectWriterException ex) when (ex.Message.Contains(\"RootObjectInstance\")) {\n    // reconcile the pre-allocated instance type with the XAML root type.\n}","preventionTips":["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."],"tags":["xaml","type-mismatch","root-instance","object-writer"],"backgroundTag":null,"analyzedSha":"04183404888ea21b4e5bfa3493e8d5f15e972c3a","analyzedAt":"2026-08-13T21:08:11.651Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}