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.2 | 104 | 2/12/2026 |
| 0.28.2-rc.65 | 49 | 1/30/2026 |
| 0.28.1 | 2,208 | 12/15/2025 |
| 0.28.1-rc.63 | 174 | 12/14/2025 |
| 0.28.0 | 27,845 | 5/2/2024 |
| 0.28.0-rc.61 | 106 | 5/2/2024 |
| 0.28.0-rc.60 | 79 | 5/2/2024 |
| 0.27.0 | 1,765 | 3/19/2024 |
| 0.27.0-rc.58 | 130 | 3/19/2024 |
| 0.27.0-rc.57 | 155 | 3/13/2024 |
| 0.26.0 | 66,235 | 10/26/2023 |
| 0.26.0-rc.55 | 141 | 11/16/2023 |
| 0.26.0-rc.53 | 153 | 10/30/2023 |
| 0.26.0-rc.52 | 120 | 10/30/2023 |
| 0.26.0-rc.51 | 132 | 10/30/2023 |
| 0.26.0-rc.50 | 115 | 10/30/2023 |
| 0.26.0-rc.48 | 160 | 10/26/2023 |
| 0.25.3 | 441 | 10/18/2023 |
| 0.25.3-rc.41 | 134 | 10/18/2023 |
| 0.5.0 | 503 | 1/15/2023 |