The MultiType.NET.SourceGenerator is a powerful CLI tool that lets you generate union types beyond the built-in Any<T1..T16> limit, including:
- ✅
Any<T1..Tn>how many types do you want? - ✅ Matching JSON converters per arity
- ✅ A factory-based JSON converter that supports them all
Install the tool globally:
dotnet tool install --global MultiType.NET.SourceGeneratorOr update it if already installed:
dotnet tool update --global MultiType.NET.SourceGeneratorFrom your solution root:
anygen --maxArity 50 --project PATH_TO_TARGET_PROJECT.csprojThis will:
- Generate
Any17,Any18, ...,Any50 - Generate corresponding JSON converters for each type
- Compatible with universal
AnyJsonConverterFactory
| Flag | Description |
|---|---|
--maxArity n |
Max number of generic types to generate (Required +17) |
--project <PATH> |
Target project csproj path, where the Any types will generated (Required) |
The generated files will be placed directly into your project:
MultiType.NET.Core/
├── Anys/Generated/
│ ├── Any17.g.cs
│ ├── ...
│ └── Any50.g.cs
├── AnySerializations/Generated/
│ ├── Any17JsonConverter.g.cs
│ ├── ...
│ └── AnyJsonConverterFactory.g.cs📌 Make sure you are installed
MultiType.NET.Corepackage.
Internally, the CLI uses:
AnyEmitter.EmitAnyTypes()to build type-safe structs- Roslyn APIs to format and normalize code
- Auto-commented headers to signal generated code
You can re-run the generator at any time. Existing .g.cs files will be overwritten.
To automate this, add a build script:
multitypegen --maxArity 50 --project ./my-poroject.csproj- The CLI must be run manually (it is not a source generator by itself).
- It works alongside
MultiType.NET.Corein the same solution. - Do not manually edit
.g.csfiles.
| Benefit | Explanation |
|---|---|
| Unbounded Unions | Supports 17+ types in Any<T1..Tn> |
| JSON Ready | Each union has a matching converter |
| API Friendly | Compatible with ASP.NET & Minimal APIs |
| Fast & Light | Outputs optimized .g.cs source code |
Go back to Deep Dive Features or start using AnyGenerator.