N.SourceGenerators.UnionTypes
0.3.0
See the version list below for details.
dotnet add package N.SourceGenerators.UnionTypes --version 0.3.0
NuGet\Install-Package N.SourceGenerators.UnionTypes -Version 0.3.0
<PackageReference Include="N.SourceGenerators.UnionTypes" Version="0.3.0" />
<PackageVersion Include="N.SourceGenerators.UnionTypes" Version="0.3.0" />
<PackageReference Include="N.SourceGenerators.UnionTypes" />
paket add N.SourceGenerators.UnionTypes --version 0.3.0
#r "nuget: N.SourceGenerators.UnionTypes, 0.3.0"
#:package N.SourceGenerators.UnionTypes@0.3.0
#addin nuget:?package=N.SourceGenerators.UnionTypes&version=0.3.0
#tool nuget:?package=N.SourceGenerators.UnionTypes&version=0.3.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 variant without creating FooResult
return new NotFoundError();
}
Explicit conversion
public ValidationError ExplicitCast(FooResult result)
{
return (ValidationError)result;
}
Match method forces you to handle all possible variations
public IActionResult MatchMethod(FooResult result)
{
return result.Match<IActionResult>(
success => new OkResult(),
validationError => new BadRequestResult(),
notFoundError => new NotFoundResult()
);
}
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 | 332 | 2/12/2026 |
| 0.28.2-rc.65 | 63 | 1/30/2026 |
| 0.28.1 | 6,082 | 12/15/2025 |
| 0.28.1-rc.63 | 182 | 12/14/2025 |
| 0.28.0 | 30,381 | 5/2/2024 |
| 0.28.0-rc.61 | 119 | 5/2/2024 |
| 0.28.0-rc.60 | 92 | 5/2/2024 |
| 0.27.0 | 1,804 | 3/19/2024 |
| 0.27.0-rc.58 | 146 | 3/19/2024 |
| 0.27.0-rc.57 | 163 | 3/13/2024 |
| 0.26.0 | 66,370 | 10/26/2023 |
| 0.26.0-rc.55 | 150 | 11/16/2023 |
| 0.26.0-rc.53 | 163 | 10/30/2023 |
| 0.26.0-rc.52 | 130 | 10/30/2023 |
| 0.26.0-rc.51 | 143 | 10/30/2023 |
| 0.26.0-rc.50 | 123 | 10/30/2023 |
| 0.26.0-rc.48 | 171 | 10/26/2023 |
| 0.25.3 | 457 | 10/18/2023 |
| 0.25.3-rc.41 | 146 | 10/18/2023 |
| 0.3.0 | 574 | 1/15/2023 |