NativeAOT and trimming

edit

The package is trimmable and compatible with NativeAOT. Reflection-based serialization is not: its methods are annotated with [RequiresUnreferencedCode] and [RequiresDynamicCode], so the compiler warns when you call them in such an application.

Use source-generated metadata

Declare a TomlSerializerContext for your types, and call the overloads that take a TomlTypeInfo<T> or the context. The overloads that take a TomlSerializerOptions are the reflection-based ones:

var config = new ServerConfig { Port = 443 };
var toml = TomlSerializer.Serialize(config, ServerContext.Default.ServerConfig);
Assert.Equal("host = \"localhost\"\nport = 443\n", toml);

var roundTrip = TomlSerializer.Deserialize(toml, ServerContext.Default.ServerConfig);
Assert.Equal(443, roundTrip!.Port);

// APIs taking a Type accept a context
var value = TomlSerializer.Deserialize(toml, typeof(ServerConfig), ServerContext.Default);
Assert.IsType<ServerConfig>(value);

See Source generation for how to declare the context.

Disable reflection

An application that only uses source-generated metadata can turn reflection-based serialization off with the MeziantouFrameworkTomlIsReflectionEnabledByDefault MSBuild property:

<PropertyGroup>
  <MeziantouFrameworkTomlIsReflectionEnabledByDefault>false</MeziantouFrameworkTomlIsReflectionEnabledByDefault>
</PropertyGroup>

When reflection is disabled, typed serialization needs a context. The built-in scalar types and the document model keep working without one.