AltaSoft.Choice.Generator
1.1.0
See the version list below for details.
dotnet add package AltaSoft.Choice.Generator --version 1.1.0
NuGet\Install-Package AltaSoft.Choice.Generator -Version 1.1.0
<PackageReference Include="AltaSoft.Choice.Generator" Version="1.1.0"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="AltaSoft.Choice.Generator" Version="1.1.0" />
<PackageReference Include="AltaSoft.Choice.Generator"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add AltaSoft.Choice.Generator --version 1.1.0
#r "nuget: AltaSoft.Choice.Generator, 1.1.0"
#addin nuget:?package=AltaSoft.Choice.Generator&version=1.1.0
#tool nuget:?package=AltaSoft.Choice.Generator&version=1.1.0
AltaSoft.ChoiceGenerator
AltaSoft.ChoiceGenerator is a lightweight C# source generator that allows you to define choice types (discriminated unions) with minimal syntax.
✨ Features
- Simple
[Choice]
attribute for defining alternatives - Generates type-safe properties
- Supports XML and System.Text.Json serialization
- Includes
CreateAsXxx
,Match
, andSwitch
methods - Auto-generates enum for valid choice types
🛠️ Installation
Add the following NuGet packages to your project:
<ItemGroup>
<PackageReference Include="AltaSoft.Choice" Version="x.x.x" />
<PackageReference Include="AltaSoft.Choice.Generator" Version="x.x.x" PrivateAssets="all" />
</ItemGroup>
✅ Define Your Choice Type
Mark your class with [Choice]
and define partial nullable properties :
using AltaSoft.Choice;
[Choice]
public sealed partial class TwoDifferentTypeChoice
{
public partial string? StringChoice { get; set; }
public partial int? IntChoice { get; set; }
}
⚙️ Generated Code
Below is the generated code for the example above:
#nullable enable
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Xml.Serialization;
public sealed partial class TwoDifferentTypeChoice
{
[XmlElement("StringChoice", typeof(string))]
[XmlElement("IntChoice", typeof(int))]
[XmlChoiceIdentifier(nameof(ChoiceType))]
[JsonIgnore]
public object Value { get; set; } = default!;
[XmlIgnore]
[JsonIgnore]
public ChoiceOf ChoiceType { get; set; }
[XmlIgnore]
[JsonInclude]
public partial string? StringChoice
{
get => ChoiceType == ChoiceOf.StringChoice ? GetAsStringChoice() : (string?)null;
set => SetAsStringChoice(value ?? throw new JsonException("Choice value cannot be null"));
}
[XmlIgnore]
[JsonInclude]
public partial int? IntChoice
{
get => ChoiceType == ChoiceOf.IntChoice ? GetAsIntChoice() : (int?)null;
set => SetAsIntChoice(value ?? throw new JsonException("Choice value cannot be null"));
}
private string GetAsStringChoice() => (string)Value;
private void SetAsStringChoice(string value)
{
Value = value;
ChoiceType = ChoiceOf.StringChoice;
}
public static TwoDifferentTypeChoice CreateAsStringChoice(string value)
{
var instance = new TwoDifferentTypeChoice();
instance.SetAsStringChoice(value);
return instance;
}
private int GetAsIntChoice() => (int)Value;
private void SetAsIntChoice(int value)
{
Value = value;
ChoiceType = ChoiceOf.IntChoice;
}
public static TwoDifferentTypeChoice CreateAsIntChoice(int value)
{
var instance = new TwoDifferentTypeChoice();
instance.SetAsIntChoice(value);
return instance;
}
public TResult Match<TResult>(
Func<string, TResult> matchStringChoice,
Func<int, TResult> matchIntChoice)
{
if (ChoiceType == ChoiceOf.StringChoice)
return matchStringChoice(StringChoice!);
if (ChoiceType == ChoiceOf.IntChoice)
return matchIntChoice(IntChoice!.Value);
throw new InvalidOperationException($"Invalid ChoiceType: {ChoiceType}");
}
public void Switch(
Action<string> matchStringChoice,
Action<int> matchIntChoice)
{
if (ChoiceType == ChoiceOf.StringChoice)
{
matchStringChoice(StringChoice!);
return;
}
if (ChoiceType == ChoiceOf.IntChoice)
{
matchIntChoice(IntChoice!.Value);
return;
}
throw new InvalidOperationException($"Invalid ChoiceType: {ChoiceType}");
}
[Serializable]
[XmlType("TwoDifferentTypeChoice__ChoiceOf")]
public enum ChoiceOf
{
[XmlEnum("StringChoice")]
StringChoice,
[XmlEnum("IntChoice")]
IntChoice,
}
}
💡 Example Usage
var choice = TwoDifferentTypeChoice.CreateAsIntChoice(123);
var result = choice.Match(
str => $"It's a string: {str}",
num => $"It's a number: {num}"
);
choice.Switch(
str => Console.WriteLine($"String: {str}"),
num => Console.WriteLine($"Number: {num}")
);
📦 Projects
AltaSoft.Choice
Contains the[Choice]
marker attributeAltaSoft.Choice.Generator
Implements the source generator that produces boilerplate code
📄 License
This project is licensed under the MIT License.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.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.