N.SourceGenerators.UnionTypes
0.5.0
See the version list below for details.
dotnet add package N.SourceGenerators.UnionTypes --version 0.5.0
NuGet\Install-Package N.SourceGenerators.UnionTypes -Version 0.5.0
<PackageReference Include="N.SourceGenerators.UnionTypes" Version="0.5.0" />
<PackageVersion Include="N.SourceGenerators.UnionTypes" Version="0.5.0" />
<PackageReference Include="N.SourceGenerators.UnionTypes" />
paket add N.SourceGenerators.UnionTypes --version 0.5.0
#r "nuget: N.SourceGenerators.UnionTypes, 0.5.0"
#:package N.SourceGenerators.UnionTypes@0.5.0
#addin nuget:?package=N.SourceGenerators.UnionTypes&version=0.5.0
#tool nuget:?package=N.SourceGenerators.UnionTypes&version=0.5.0
N.SourceGenerators.UnionTypes
Discriminated union type source generator
Motivation
C# doesn't support discriminated unions yet. This source generator helps automate writing union types.
Using
Add package reference to N.SourceGenerators.UnionTypes
dotnet add package N.SourceGenerators.UnionTypes
Create a partial class that will be used as a union type
public partial class FooResult
{
}
Add types you want to use in a discriminated union
public record Success(int Value);
public record ValidationError(string Message);
public record NotFoundError;
public partial class FooResult
{
}
Add N.SourceGenerators.UnionTypes.UnionTypeAttribute to a union type.
using N.SourceGenerators.UnionTypes;
public record Success(int Value);
public record ValidationError(string Message);
public record NotFoundError;
[UnionType(typeof(Success))]
[UnionType(typeof(ValidationError))]
[UnionType(typeof(NotFoundError))]
public partial class FooResult
{
}
Examples
All examples can be found in examples project
Implicit conversion
public FooResult ImplicitReturn()
{
// you can return any union type variation without creating FooResult
return new NotFoundError();
}
Explicit conversion
public ValidationError ExplicitCast(FooResult result)
{
return (ValidationError)result;
}
Match and MatchAsync methods force you to handle all possible variations
public IActionResult MatchMethod(FooResult result)
{
return result.Match<IActionResult>(
success => new OkResult(),
validationError => new BadRequestResult(),
notFoundError => new NotFoundResult()
);
}
public async Task<IActionResult> MatchAsyncMethod(FooResult result, CancellationToken cancellationToken)
{
return await result.MatchAsync<IActionResult>(
static async (success, ct) =>
{
await SomeWork(ct);
return new OkResult();
}, static async (validationError, ct) =>
{
await SomeWork(ct);
return new BadRequestResult();
}, static async (notFoundError, ct) =>
{
await SomeWork(ct);
return new NotFoundResult();
}, cancellationToken);
static Task SomeWork(CancellationToken ct)
{
return Task.Delay(100, ct);
}
}
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.28.1 | 238 | 12/15/2025 |
| 0.28.1-rc.63 | 163 | 12/14/2025 |
| 0.28.0 | 20,986 | 5/2/2024 |
| 0.28.0-rc.61 | 84 | 5/2/2024 |
| 0.28.0-rc.60 | 52 | 5/2/2024 |
| 0.27.0 | 1,725 | 3/19/2024 |
| 0.27.0-rc.58 | 109 | 3/19/2024 |
| 0.27.0-rc.57 | 132 | 3/13/2024 |
| 0.26.0 | 65,844 | 10/26/2023 |
| 0.26.0-rc.55 | 116 | 11/16/2023 |
| 0.26.0-rc.53 | 128 | 10/30/2023 |
| 0.26.0-rc.52 | 99 | 10/30/2023 |
| 0.26.0-rc.51 | 110 | 10/30/2023 |
| 0.26.0-rc.50 | 97 | 10/30/2023 |
| 0.26.0-rc.48 | 138 | 10/26/2023 |
| 0.25.3 | 421 | 10/18/2023 |
| 0.25.3-rc.41 | 111 | 10/18/2023 |
| 0.25.1-rc.40 | 364 | 10/17/2023 |
| 0.25.0-rc.39 | 122 | 10/17/2023 |
| 0.24.0 | 1,082 | 10/17/2023 |
| 0.24.0-rc.37 | 179 | 10/16/2023 |
| 0.23.0 | 769 | 10/3/2023 |
| 0.23.0-rc.35 | 342 | 10/2/2023 |
| 0.22.0 | 376 | 10/2/2023 |
| 0.22.0-rc.33 | 113 | 10/2/2023 |
| 0.21.0 | 337 | 10/2/2023 |
| 0.21.0-rc.31 | 134 | 10/2/2023 |
| 0.21.0-rc.30 | 113 | 9/28/2023 |
| 0.20.0-rc.29 | 953 | 9/6/2023 |
| 0.20.0-rc.28 | 136 | 9/6/2023 |
| 0.19.0 | 752 | 8/3/2023 |
| 0.18.0 | 632 | 2/5/2023 |
| 0.17.0 | 477 | 2/2/2023 |
| 0.16.0 | 440 | 1/30/2023 |
| 0.15.0 | 474 | 1/29/2023 |
| 0.14.0 | 461 | 1/21/2023 |
| 0.13.0 | 514 | 1/20/2023 |
| 0.12.0 | 509 | 1/20/2023 |
| 0.11.0 | 482 | 1/18/2023 |
| 0.10.0 | 509 | 1/18/2023 |
| 0.9.0 | 504 | 1/17/2023 |
| 0.8.0 | 460 | 1/17/2023 |
| 0.7.0 | 469 | 1/17/2023 |
| 0.6.0 | 480 | 1/15/2023 |
| 0.5.1 | 472 | 1/15/2023 |
| 0.5.0 | 485 | 1/15/2023 |
| 0.4.0 | 532 | 1/15/2023 |
| 0.3.0 | 546 | 1/15/2023 |
| 0.2.0 | 542 | 1/15/2023 |
| 0.1.0 | 583 | 1/15/2023 |