Skip to content
/ AnyOf Public

Use the AnyOf<TFirst, TSecond, ...> type to handle multiple defined types as input parameters or return values for methods.

License

Notifications You must be signed in to change notification settings

StefH/AnyOf

Repository files navigation

AnyOf

Use the AnyOf<First, TSecond, ...> type to handle multiple defined types as input parameters for methods.

This project uses code generation to generate up to 10 AnyOf-types:

  • AnyOf<TFirst, TSecond>
  • AnyOf<TFirst, TSecond, TThird>
  • AnyOf<TFirst, TSecond, TThird, TFourth>
  • ...

Install

The normal version:

NuGet Badge

The source-generator version:

NuGet Badge

AnyOf.Newtonsoft.Json

This package can be used to serialize/deserialize (with Newtonsoft.Json) an object which contains an AnyOf-type.
For more details see wiki : AnyOf.Newtonsoft.Json

NuGet Badge

AnyOf.System.Text.Json

This package can be used to serialize/deserialize (with System.Text.Json) an object which contains an AnyOf-type.
For more details see wiki : AnyOf.System.Text.Json

NuGet Badge

Usage

using System;
using AnyOfTypes;

namespace ConsoleAppConsumer
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Console.WriteLine(ReturnSomething().CurrentValue);

            X(42);
            X("test");
        }

        // This method returns an string, int or bool in a random way.
        private static AnyOf<string, int, bool> ReturnSomething()
        {
            return new Random().Next(3) switch
            {
                1 => "test",
                2 => 42,
                _ => true,
            };
        }

        // This method accepts only an int and a string.
        private static void X(AnyOf<int, string> value)
        {
            Console.WriteLine("ToString " + value.ToString());
            Console.WriteLine("CurrentValue " + value.CurrentValue);
            Console.WriteLine("IsUndefined " + value.IsUndefined);
            Console.WriteLine("IsFirst " + value.IsFirst);
            Console.WriteLine("IsSecond " + value.IsSecond);

            switch (value.CurrentType)
            {
                case AnyOfType.First:
                    Console.WriteLine("AnyOfType = First with value " + value.First);
                    break;

                case AnyOfType.Second:
                    Console.WriteLine("AnyOfType = Second with value " + value.Second);
                    break;

                default:
                    Console.WriteLine("???");
                    break;
            }
        }
    }
}

Sponsors

Entity Framework Extensions and Dapper Plus are major sponsors and proud to contribute to the development of AnyOf, AnyOf.Newtonsoft.Json and AnyOf.System.Text.Json.

Entity Framework Extensions

Dapper Plus

About

Use the AnyOf<TFirst, TSecond, ...> type to handle multiple defined types as input parameters or return values for methods.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

  •  

Contributors 3

  •  
  •  
  •  
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy