{"record":{"id":"231cb3c7051f40e7","repo":"unoplatform/uno","slug":"instance-type-0-must-be-public-and-non-nested","errorCode":null,"errorMessage":"instance type '{0}' must be public and non-nested.","messagePattern":"instance type '(.+?)' must be public and non-nested\\.","errorType":"exception","errorClass":"XamlObjectReaderException","httpStatus":null,"severity":"error","filePath":"src/SourceGenerators/System.Xaml/System.Xaml/XamlObjectReader.cs","lineNumber":96,"sourceCode":"\t\t\tif (schemaContext == null)\n\t\t\t\tthrow new ArgumentNullException (\"schemaContext\");\n\t\t\t// FIXME: special case? or can it be generalized? In .NET, For Type instance Instance returns TypeExtension at root StartObject, while for Array it remains to return Array.\n\t\t\tif (instance is Type)\n\t\t\t\tinstance = new TypeExtension ((Type) instance);\n\n\t\t\t// See also Instance property for this weirdness.\n\t\t\tthis.root_raw = instance;\n\t\t\tinstance = TypeExtensionMethods.GetExtensionWrapped (instance);\n\t\t\tthis.root = instance;\n\n\t\t\tsctx = schemaContext;\n//\t\t\tthis.settings = settings;\n\n\t\t\t// check type validity. Note that some checks also needs done at Read() phase. (it is likely FIXME:)\n\t\t\tif (instance != null) {\n\t\t\t\tvar type = new InstanceContext (instance).GetRawValue ().GetType ();\n\t\t\t\tif (!type.IsPublic)\n\t\t\t\t\tthrow new XamlObjectReaderException (String.Format (CultureInfo.InvariantCulture, \"instance type '{0}' must be public and non-nested.\", type));\n\t\t\t\tvar xt = SchemaContext.GetXamlType (type);\n\t\t\t\tif (xt.ConstructionRequiresArguments && !xt.GetConstructorArguments ().Any () && xt.TypeConverter == null)\n\t\t\t\t\tthrow new XamlObjectReaderException (String.Format (CultureInfo.InvariantCulture, \"instance type '{0}' has no default constructor.\", type));\n\t\t\t}\n\n\t\t\tvalue_serializer_context = new ValueSerializerContext (new PrefixLookup (sctx), sctx, null);\n\t\t\tnew XamlObjectNodeIterator (instance, sctx, value_serializer_context).PrepareReading ();\n\t\t}\n\t\t\n\t\tbool is_eof;\n\t\tobject root, root_raw;\n\t\tXamlSchemaContext sctx;\n//\t\tXamlObjectReaderSettings settings;\n\t\tIValueSerializerContext value_serializer_context;\n\n\t\tIEnumerator<NamespaceDeclaration> ns_iterator;\n\t\tIEnumerator<XamlNodeInfo> nodes;\n","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/SourceGenerators/System.Xaml/System.Xaml/XamlObjectReader.cs#L78-L114","documentation":"Thrown by the XamlObjectReader constructor when the type of the instance object is not public (including nested types). XamlObjectReader uses reflection and XAML requires public, non-nested types for serialization so that the type can be referenced from markup and instantiated by name; a non-public type cannot be meaningfully written to XAML.","triggerScenarios":"Constructing new XamlObjectReader(instance, sctx) where instance.GetType().IsPublic is false. This includes private, internal, protected, and nested classes.","commonSituations":"Serializing internal helper classes, anonymous types, or nested classes through XamlObjectReader. Passing a domain-model object that was declared internal for encapsulation. Attempting to round-trip test fixtures or DTOs that are not public.","solutions":["Make the instance type public and non-nested so it can be serialized to XAML.","If the type must stay internal, map it to a public XAML surrogate type via a custom XamlSchemaContext or TypeConverter.","Do not pass non-public types to XamlObjectReader; serialize a public wrapper/proxy instead."],"exampleFix":"// before\ninternal class MyInternalData { public string Value { get; set; } }\nvar reader = new XamlObjectReader(new MyInternalData { Value = \"x\" }, sctx);\n\n// after\npublic class MyPublicData { public string Value { get; set; } }\nvar reader = new XamlObjectReader(new MyPublicData { Value = \"x\" }, sctx);","handlingStrategy":"validation","validationCode":"var type = instance.GetType();\nif (!type.IsPublic)\n    throw new InvalidOperationException($\"{type} must be public for XAML serialization.\");\nvar reader = new XamlObjectReader(instance, sctx);","typeGuard":"static bool IsXamlSerializableType(Type t) => t.IsPublic && !t.IsNested;","tryCatchPattern":"try { var reader = new XamlObjectReader(obj, sctx); }\ncatch (XamlObjectReaderException) { /* map to a public surrogate type */ }","preventionTips":["Only serialize public, non-nested types through XamlObjectReader.","Map internal types to public surrogates if encapsulation is required."],"tags":["xaml","object-reader","reflection","type-visibility","uno-xaml"],"backgroundTag":null,"analyzedSha":"04183404888ea21b4e5bfa3493e8d5f15e972c3a","analyzedAt":"2026-08-13T21:08:11.651Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}