Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/StefH/AnyOf
Use the AnyOf<TFirst, TSecond, ...> type to handle multiple defined types as input parameters or return values for methods.
https://github.com/StefH/AnyOf
anyof multiple type typed types
Last synced: 3 months ago
JSON representation
Use the AnyOf<TFirst, TSecond, ...> type to handle multiple defined types as input parameters or return values for methods.
- Host: GitHub
- URL: https://github.com/StefH/AnyOf
- Owner: StefH
- License: mit
- Created: 2021-07-04T12:49:51.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-02-10T13:28:40.000Z (9 months ago)
- Last Synced: 2024-04-12T16:44:17.099Z (7 months ago)
- Topics: anyof, multiple, type, typed, types
- Language: C#
- Homepage:
- Size: 108 KB
- Stars: 101
- Watchers: 8
- Forks: 6
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- csharp-source-generators - AnyOf - ![stars](https://img.shields.io/github/stars/StefH/AnyOf?style=flat-square&cacheSeconds=604800) ![last commit](https://img.shields.io/github/last-commit/StefH/AnyOf?style=flat-square&cacheSeconds=86400) The Source Generator creates a `AnyOf<First, TSecond, ...>` type to handle multiple defined types as input parameters for methods. (Source Generators / Functional Programming)
- RSCG_Examples - AnyOf
README
# AnyOf
Use the `AnyOf` type to handle multiple defined types as input parameters for methods.This project uses code generation to generate up to 10 AnyOf-types:
- `AnyOf`
- `AnyOf`
- `AnyOf`
- ...# Install
## The normal version:
[![NuGet Badge](https://buildstats.info/nuget/AnyOf)](https://www.nuget.org/packages/AnyOf)## The source-generator version:
[![NuGet Badge](https://buildstats.info/nuget/AnyOf.SourceGenerator)](https://www.nuget.org/packages/AnyOf.SourceGenerator)## 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](https://github.com/StefH/AnyOf/wiki/AnyOf.Newtonsoft.Json)[![NuGet Badge](https://buildstats.info/nuget/AnyOf.Newtonsoft.Json)](https://www.nuget.org/packages/AnyOf.Newtonsoft.Json)
## 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](https://github.com/StefH/AnyOf/wiki/AnyOf.System.Text.Json)[![NuGet Badge](https://buildstats.info/nuget/AnyOf.System.Text.Json)](https://www.nuget.org/packages/AnyOf.System.Text.Json)
# Usage
``` c#
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 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 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;
}
}
}
}
```