---
title: "Register multiple YAML types for source generation in C#"
url: https://recursiveai.org/oneyaml/csharp/cookbook/register-yaml-contracts-for-multiple-types/
description: "Register two or more C# types in one OneYaml source-generated context, then deserialize each YAML input with its own generated contract."
version: "1.2.1"
---

# Register multiple YAML types for source generation in C#

**OneYaml** is a free, open-source YAML library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/OneYaml) · [GitHub](https://github.com/recursive-ai-oss/oneyaml-csharp) · [Docs](https://recursiveai.org/oneyaml/csharp/docs/) · [Download example (.zip)](https://recursiveai.org/oneyaml/csharp/cookbook/register-yaml-contracts-for-multiple-types/register-yaml-contracts-for-multiple-types.zip)

Add one `[YamlSerializable]` attribute per root type on the same context. OneYaml generates the metadata needed to read and write each type.

```shell
dotnet add package OneYaml
```

The library targets .NET 8 and later; the example project uses [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0).

```csharp
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](https://recursiveai.org/oneyaml/csharp/cookbook/register-yaml-contracts-for-multiple-types/register-yaml-contracts-for-multiple-types.zip) and `dotnet run`.

Prints:

```text
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`:

```csharp
Console.Write(YamlSerializer.Serialize(
    server, types.Server));
```

## Related recipes

- [Parse YAML into C# classes](https://recursiveai.org/oneyaml/csharp/cookbook/parse-yaml-into-classes/)
- [Use YAML in a Native AOT app](https://recursiveai.org/oneyaml/csharp/cookbook/native-aot/)

## 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](https://recursiveai.org/oneyaml/csharp/cookbook/deserialize-polymorphic-yaml/), 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](https://recursiveai.org/oneyaml/csharp/docs/serializer/#source-generation-and-native-aot) 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.
