{"record":{"id":"4d606073c72510bd","repo":"tui-cs/Terminal.Gui","slug":"cannot-create-instance-of-type-type-fullname-no","errorCode":null,"errorMessage":"Cannot create instance of type {type.FullName}. No parameterless constructor or clone method found.","messagePattern":"Cannot create instance of type (.+?)\\. No parameterless constructor or clone method found\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Terminal.Gui/Configuration/DeepCloner.cs","lineNumber":182,"sourceCode":"        {\n            // Try parameterless constructor\n            if (type.GetConstructor (Type.EmptyTypes) != null)\n            {\n                return Activator.CreateInstance (type)!;\n            }\n\n            // In AOT, try using the JsonSerializer if available\n            if (IsAotEnvironment ())\n            {\n                JsonTypeInfo? jsonTypeInfo = TuiSerializerContext.Instance.GetTypeInfo (type);\n\n                if (jsonTypeInfo is not null)\n                {\n                    return JsonSerializer.Deserialize (\"{}\", jsonTypeInfo)!;\n                }\n            }\n\n            throw new InvalidOperationException ($\"Cannot create instance of type {type.FullName}. No parameterless constructor or clone method found.\");\n        }\n        catch (MissingMethodException)\n        {\n            throw new InvalidOperationException (\n                                                 $\"Cannot create instance of type {type.FullName} in AOT context. Consider adding this type to your SourceGenerationContext.\");\n        }\n    }\n\n    private static object CloneArray (object source, ConcurrentDictionary<object, object> visited)\n    {\n        Array array = (Array)source;\n        Array newArray = (Array)array.Clone ();\n        visited.TryAdd (source, newArray);\n\n        for (var i = 0; i < array.Length; i++)\n        {\n            object? value = array.GetValue (i);\n            object? clonedValue = DeepCloneInternal (value, visited);","sourceCodeStart":164,"sourceCodeEnd":200,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/Configuration/DeepCloner.cs#L164-L200","documentation":"Thrown by DeepCloner.CreateInstance when a type has no public parameterless constructor and (in AOT) no JsonTypeInfo is available to deserialize \"{}\". DeepCloner is used by ConfigProperty.Apply to deep-clone property values before writing them; if the value's runtime type cannot be instantiated, cloning fails. This is an InvalidOperationException about the cloned type, not the config value itself.","triggerScenarios":"A [ConfigurationProperty] holds a value whose type lacks a parameterless constructor (e.g. a struct with only a parameterized ctor, a record with required init properties and no default ctor, or a type not registered for AOT source generation).","commonSituations":"Adding a custom configuration property whose type has no default constructor; running under NativeAOT/trimming where reflection-based instantiation is unavailable and the type was not added to TuiSerializerContext.","solutions":["Add a public parameterless constructor to the type held by the config property.","If the type is immutable by design, give it ICloneable or a Clone() method (DeepCloner checks for those first), or implement a copy constructor DeepCloner can find.","Under NativeAOT, add the type to your SourceGenerationContext / TuiSerializerContext so the JsonSerializer fallback works.","Avoid storing types DeepCloner cannot handle as [ConfigurationProperty] values."],"exampleFix":"// before\npublic class MyConfig { public MyConfig(int x) {} }\n// after\npublic class MyConfig\n{\n    public MyConfig () {}\n    public MyConfig (int x) {}\n}","handlingStrategy":"validation","validationCode":"// Reject config property types without a parameterless ctor at registration time.\nType t = configProperty.PropertyInfo!.PropertyType;\nif (t.GetConstructor (Type.EmptyTypes) is null\n    && typeof (ICloneable).IsAssignableFrom (t) == false)\n    throw new InvalidOperationException ($\"{t} has no parameterless ctor or Clone; DeepCloner will fail.\");","typeGuard":"static bool IsDeepCloneable (Type t) =>\n    t.GetConstructor (Type.EmptyTypes) is not null\n    || typeof (ICloneable).IsAssignableFrom (t)\n    || t.GetMethod (\"Clone\", Type.EmptyTypes) is not null;","tryCatchPattern":"try { ConfigurationManager.Apply (); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains (\"No parameterless constructor\"))\n{ /* add a default ctor to the offending type */ }","preventionTips":["Give every config value type a public parameterless constructor or a Clone method.","Unit-test DeepCloner.DeepClone on each config type."],"tags":["configuration","deepcloner","aot","reflection","instantiation"],"backgroundTag":null,"analyzedSha":"2e47b11478db083499917f2ad27c34d30efb0df1","analyzedAt":"2026-08-13T19:20:08.826Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}