{"record":{"id":"2f003cdcb79b00bd","repo":"unoplatform/uno","slug":"instance-type-0-has-no-default-constructor","errorCode":null,"errorMessage":"instance type '{0}' has no default constructor.","messagePattern":"instance type '(.+?)' has no default constructor\\.","errorType":"exception","errorClass":"XamlObjectReaderException","httpStatus":null,"severity":"error","filePath":"src/SourceGenerators/System.Xaml/System.Xaml/XamlObjectReader.cs","lineNumber":99,"sourceCode":"\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\n\t\tPrefixLookup PrefixLookup {\n\t\t\tget { return (PrefixLookup) value_serializer_context.GetService (typeof (INamespacePrefixLookup)); }\n\t\t}","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/SourceGenerators/System.Xaml/System.Xaml/XamlObjectReader.cs#L81-L117","documentation":"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.","triggerScenarios":"Constructing new XamlObjectReader(instance, sctx) where the type's only constructors require parameters and the type has neither [TypeConverter] nor [ConstructorArgument] metadata.","commonSituations":"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.","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."],"exampleFix":"// before\npublic class MyData\n{\n    public MyData(string value) { Value = value; }\n    public string Value { get; set; }\n}\nvar reader = new XamlObjectReader(new MyData(\"x\"), sctx); // throws\n\n// after\npublic class MyData\n{\n    public MyData() { } // parameterless ctor added\n    public MyData(string value) { Value = value; }\n    public string Value { get; set; }\n}","handlingStrategy":"validation","validationCode":"var type = instance.GetType();\nif (type.GetConstructor(Type.EmptyTypes) == null\n    && !HasConstructorArguments(type)\n    && TypeDescriptor.GetConverter(type) == null)\n    throw new InvalidOperationException($\"{type} is not XAML-constructible.\");","typeGuard":"static bool IsXamlConstructible(Type t)\n    => t.GetConstructor(Type.EmptyTypes) != null\n       || t.GetCustomAttribute<TypeConverterAttribute>() != null;","tryCatchPattern":"try { var reader = new XamlObjectReader(obj, sctx); }\ncatch (XamlObjectReaderException) { /* add ctor or TypeConverter */ }","preventionTips":["Ensure serializable types have a parameterless constructor or a TypeConverter.","Add [TypeConverter] for immutable types that cannot add a default constructor."],"tags":["xaml","object-reader","constructor","type-converter","uno-xaml"],"backgroundTag":null,"analyzedSha":"04183404888ea21b4e5bfa3493e8d5f15e972c3a","analyzedAt":"2026-08-13T21:08:11.651Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}