Add one [YamlSerializable] attribute per root type on the same context. OneYaml generates the metadata needed to read and write each type.
dotnet add package OneYamlThe library targets .NET 8 and later; the example project uses .NET 10.
using OneYaml; [YamlSerializable(typeof(Server))] [YamlSerializable(typeof(Worker))] partial class AppYamlContext : YamlSerializerContext; record Server(int Port); record Worker(int Retries); static class Program { static void Main() { var types = AppYamlContext.Default; var server = YamlSerializer .Deserialize("port: 8080", types.Server)!; var worker = YamlSerializer .Deserialize("retries: 3", types.Worker)!; Console.WriteLine( $"Port: {server.Port}"); Console.WriteLine( $"Retries: {worker.Retries}"); } }
Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.
Prints:
Port: 8080 Retries: 3
If the context class isn’t partial, the generator reports a build diagnostic; it can’t add the generated properties to a non-partial class.
Variations
The same generated contract can write YAML. Add this call at the end of Main:
Console.Write(YamlSerializer.Serialize( server, types.Server));
Related recipes
The short answer
Put [YamlSerializable(typeof(Server))] and [YamlSerializable(typeof(Worker))] on the same partial class derived from YamlSerializerContext. The generator produces a YamlTypeInfo<T> contract for each type: metadata the serializer uses to read or write that type. Pass AppYamlContext.Default.Server when reading server settings and AppYamlContext.Default.Worker when reading worker settings. There’s no separate generator package or build property to add. The project’s package reference is <PackageReference Include="OneYaml" Version="1.2.1" />.
How do I deserialize YAML into different C# types?
Choose the contract based on what your application expects at the call site. Here the caller knows which text holds the server port and which holds the worker retry count. The shared context doesn’t look at the YAML and guess a type. It gives you the contracts to pass when you already know which kind of value you’re reading. That’s different from polymorphic deserialization, where a declared base type can have several derived types.
Register the types you use as roots. OneYaml generates metadata for the types those roots reach, too. If a server record holds another record, you don’t need another attribute just to make that member bind. A second independent settings file with a different root record is a good reason to add a second attribute. The attributes belong on the context, not on the settings records.
Does YAML source generation work with Native AOT?
Yes. Pass the generated contract instead of calling the overload that binds with reflection. Reflection-based binding is off by default in apps published with PublishAot or PublishTrimmed. In a single app, using one context for independent configuration types gives you one place to see which types you intend to load. The example runs as a regular console app; the generated-contract calls are the same ones you’d use in a Native AOT build.
For options beyond the defaults, the serializer guide shows how to construct the context with options or set it as the TypeInfoResolver. Set the options on the context or resolver path you actually use; passing a generated property from Default uses that context’s defaults.