AltaSoft.Choice.Generator 1.1.0

There is a newer version of this package available.
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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AltaSoft.Choice.Generator" Version="1.1.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AltaSoft.Choice.Generator" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="AltaSoft.Choice.Generator">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AltaSoft.Choice.Generator --version 1.1.0
                    
#r "nuget: AltaSoft.Choice.Generator, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=AltaSoft.Choice.Generator&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=AltaSoft.Choice.Generator&version=1.1.0
                    
Install as a Cake Tool

AltaSoft.ChoiceGenerator

NuGet - AltaSoft.Choice NuGet - AltaSoft.Choice.Generator Dot NET 8+

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, and Switch 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 attribute

  • AltaSoft.Choice.Generator
    Implements the source generator that produces boilerplate code


📄 License

This project is licensed under the MIT License.

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
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
1.3.1 261 5/16/2025
1.3.0 193 5/16/2025
1.2.3 220 5/15/2025
1.2.2 234 5/14/2025
1.2.1 147 5/8/2025
1.2.0 150 5/7/2025
1.1.0 90 5/3/2025