tui-cs/Terminal.Gui · error · InvalidOperationException
Cannot create instance of type {type.FullName} in AOT contex
Error message
Cannot create instance of type {type.FullName} in AOT context. Consider adding this type to your SourceGenerationContext. What it means
Thrown by DeepCloner.CreateInstance's catch (MissingMethodException): in an AOT/trimmed environment the type could not be instantiated because its metadata was missing (no constructor retained after trimming). The message points the user at SourceGenerationContext as the remedy. Distinct from error 36 which fires when no ctor exists at all; this fires when the ctor existed but was trimmed away.
Source
Thrown at Terminal.Gui/Configuration/DeepCloner.cs:186
return Activator.CreateInstance (type)!;
}
// In AOT, try using the JsonSerializer if available
if (IsAotEnvironment ())
{
JsonTypeInfo? jsonTypeInfo = TuiSerializerContext.Instance.GetTypeInfo (type);
if (jsonTypeInfo is not null)
{
return JsonSerializer.Deserialize ("{}", jsonTypeInfo)!;
}
}
throw new InvalidOperationException ($"Cannot create instance of type {type.FullName}. No parameterless constructor or clone method found.");
}
catch (MissingMethodException)
{
throw new InvalidOperationException (
$"Cannot create instance of type {type.FullName} in AOT context. Consider adding this type to your SourceGenerationContext.");
}
}
private static object CloneArray (object source, ConcurrentDictionary<object, object> visited)
{
Array array = (Array)source;
Array newArray = (Array)array.Clone ();
visited.TryAdd (source, newArray);
for (var i = 0; i < array.Length; i++)
{
object? value = array.GetValue (i);
object? clonedValue = DeepCloneInternal (value, visited);
newArray.SetValue (clonedValue, i);
}
return newArray;View on GitHub (pinned to 2e47b11478)
Solutions
- Add the type to a JsonSerializerSourceGenerationContext (e.g. TuiSerializerContext) so DeepCloner's JsonSerializer fallback can deserialize "{}".
- Add a DynamicDependency or trim-root descriptor to preserve the type's constructor.
- If using NativeAOT, add <TrimmerRootDescriptor> or root the assembly.
- Add a public parameterless constructor explicitly so the trimmer keeps it.
Example fix
// Register the type for source generation so AOT can instantiate it
[JsonSourceGenerationOptions (WriteIndented = true)]
[JsonSerializable (typeof (MyConfigType))]
internal partial class TuiSerializerContext : JsonSerializerContext { } Defensive patterns
Strategy: validation
Validate before calling
// For AOT, confirm the type is registered in the source-gen context.
JsonTypeInfo? info = TuiSerializerContext.Instance.GetTypeInfo (typeof (MyConfigType));
if (info is null)
throw new InvalidOperationException ($"{typeof (MyConfigType)} is not source-generated; AOT clone will fail."); Type guard
static bool IsAotCloneable (Type t) =>
TuiSerializerContext.Instance.GetTypeInfo (t) is not null
|| t.GetConstructor (Type.EmptyTypes) is not null; Try / catch
try { ConfigurationManager.Apply (); }
catch (InvalidOperationException ex) when (ex.Message.Contains ("AOT context"))
{ /* add the type to SourceGenerationContext and republish */ } Prevention
- Register every config value type in a JsonSerializerSourceGenerationContext.
- Run a NativeAOT publish in CI to surface trimming gaps early.
When it happens
Trigger: NativeAOT or trimmed publish where a config property's type was not rooted and its constructor was removed by the trimmer; Activator.CreateInstance throws MissingMethodException.
Common situations: Publishing with ReadyToRun/NativeAOT without registering custom configuration types; upgrading trimming aggressiveness; a config type living in a satellite assembly.
Related errors
- Cannot create instance of type {type.FullName}. No parameter
- Cloning of collection type {type.Name} is not supported with
- {p.Key}: Unsupported configuration converter type "{converte
- Cloning of immutable collections like {type.Name} is not sup
- Cloning of collection type {type.Name} is not supported unle
AI-assisted analysis of tui-cs/Terminal.Gui@2e47b11478 (2026-08-13).
Data as JSON: /api/errors/e1a9779a04be6b52.
Report an issue: GitHub.