MultiTypeParameterGenerator 1.1.2

dotnet add package MultiTypeParameterGenerator --version 1.1.2
                    
NuGet\Install-Package MultiTypeParameterGenerator -Version 1.1.2
                    
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="MultiTypeParameterGenerator" Version="1.1.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MultiTypeParameterGenerator" Version="1.1.2" />
                    
Directory.Packages.props
<PackageReference Include="MultiTypeParameterGenerator" />
                    
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 MultiTypeParameterGenerator --version 1.1.2
                    
#r "nuget: MultiTypeParameterGenerator, 1.1.2"
                    
#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.
#:package MultiTypeParameterGenerator@1.1.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MultiTypeParameterGenerator&version=1.1.2
                    
Install as a Cake Addin
#tool nuget:?package=MultiTypeParameterGenerator&version=1.1.2
                    
Install as a Cake Tool

MultiTypeParameterGenerator

NuGet License GitHub Build

A C# source generator that creates method variants with multiple types for a single parameter, reducing boilerplate code and enabling type-safe APIs with broad type support.

Overview

MultiTypeParameterGenerator helps you avoid writing repetitive code when you need methods that accept multiple types for the same parameter. Instead of manually creating overloads for each type, this generator automatically creates them for you based on simple annotations.

Installation

Install the NuGet package in your project:

dotnet add package MultiTypeParameterGenerator

Or add it directly to your .csproj file:

<ItemGroup>
    <PackageReference Include="MultiTypeParameterGenerator" Version="1.1.0" />
</ItemGroup>

Usage

Basic Example

Add an [AccessModifiers] attribute to your method and an [AcceptedTypes] attribute to your generic type parameter:

using MultiTypeParameterGenerator;

public partial class Calculator
{
    [AccessModifiers(Public)] private T Add<int, [AcceptedTypes<int, double>] T>(int first, T second)
    {
        // Implementation
        return first + second;
    }
}

The generator will create method overloads for each specified type:

// Generated code
partial class Calculator
{
    public int Add(int first, int value) => Add<int>(first, second);
    
    public double Add(int first, double value) => Add<double>(first, second);
}

Multiple Parameters

You can specify accepted types for multiple parameters to generate overloads for different combinations:

public void Plot<[AcceptedTypes<int, double>] T1, [AcceptedTypes<int, double>] T2>(T1 x, T2 y)
{
    // Implementation
}

// Generates:
// public void Plot(int x, int y);
// public void Plot(int x, double y);
// public void Plot(double x, int y);
// public void Plot(double x, double y);

XML documentation comments

If you add summary comments they will not get lost:

/// <summary>
///     Calculates sum of two values
/// </summary>
/// <param name="first">First summand</param>
/// <param name="second">Second summand</param>
/// <typeparam name="T1">Type of first summand</typeparam>
/// <typeparam name="T2">Type of second summand</typeparam>
/// <returns>sum of both values</returns>
private T Add<int, [AcceptedTypes<int, double>] T>(int first, T second)
{
    // Implementation
    return first + second;
}

The generated code will contain inheritdoc comments:

// Generated code
partial class Calculator
{
    /// <inheritdoc cref="Add{T}(int, T)" />
    public int Add(int first, int value) => Add<int>(first, second);

    /// <inheritdoc cref="Add{T}(int, T)" />
    public double Add(int first, double value) => Add<double>(first, second);
}

Advanced Usage

Custom Type Names

You can also provide additional type names as strings:

public void Process<[AcceptedTypes<int, double>("System.DateTime", "System.DateOnly")] T>(T value)
{
    // Implementation
}

// Generates overloads for int, double, System.DateTime and System.DateOnly
Generic Types

Generate methods with generic type parameters:

public void Handle<[AcceptedTypes<GenericType<Foo>, string, FooBar>] T>(T data)
{
    // Implementation
}

// Generates:
// public void Handle<TFoo>(TFoo data) where TFoo : Foo;
// public void Handle(string data);
// public void Handle(FooBar data);

You can also define that generic type parameters should be used for every accepted reference type:

public void Handle<[AcceptedTypes<Foo, string, FooBar>(true)] T>(T data)
{
    // Implementation
}

// Generates:
// public void Handle<TFoo>(TFoo data) where TFoo : Foo;
// public void Handle(string data);
// public void Handle<TFooBar>(TFooBar data) where TFooBar : FooBar;
Shared Accepted Type Collections

If you need the same accepted types for multiple methods, you do not need to specify them multiple times. Simply use an AcceptedTypesCollection and give it a (global) type alias:

using AcceptedTypesOfCalculator = AcceptedTypesCollection<int, double>

public partial class Calculator
{
    public T Add<int, [AcceptedTypes<AcceptedTypesOfCalculator>] T>(int first, T second)
    {
        // Implementation
        return first + second;
    }

    public T Subtract<int, [AcceptedTypes<AcceptedTypesOfCalculator>] T>(int first, T second)
    {
        // Implementation
        return first - second;
    }
}

// Generates:
// public int Add(int first, int value);
// public double Add(int first, double value);
// public int Subtract(int first, int value);
// public double Subtract(int first, double value);

Attributes Reference

AcceptedTypesAttribute

The main attribute to specify which types are valid for a generic type parameter. Available in various generic versions to support multiple type parameters. Basic syntax:

[AcceptedTypes<T1, T2, ...>]

Parameters:

  • Generic type parameters: Types to generate overloads for
  • (optional) AsGenericTypes: Whether all provided reference types should be treated as generic type arguments
  • (optional) AdditionalTypes: Additional type names as strings

Example:

[AcceptedTypes<int, double>(AsGenericTypes: false, AdditionalTypes: "System.DateTime")]

AccessModifiersAttribute

The attribute to specify which access modifiers that the generate overloads will have. Basic syntax:

[AccessModifiers(AccessModifiers: MultiTypeParameterGenerator.Public)]

Parameters:

  • AccessModifiers: Access modifiers that the generate overloads will have
    public enum AccessModifier
    {
        Public,
        Protected,
        Internal,
        ProtectedInternal,
        PrivateProtected
    }
    

Example:

[AccessModifiers(ProtectedInternal)] private void Foo<[AcceptedTypes<int, double>] T>(T value);

Example Use Cases

Numerical Operations

public class MathUtility
{
    public T Square<[AcceptedTypes<int, double, decimal>] T>(T value)
    {
        // Implementation
        return value * value;
    }
}

String Handling

public class StringProcessor
{
    public string Normalize<[AcceptedTypes<string, char[]>] T>(T input)
    {
        // Implementation
        return input.ToString();
    }
}

Performance Considerations

  • The generated code is created at compile-time, so there's no runtime performance impact
  • Generated methods use direct type handling rather than boxing/unboxing
  • Code size may increase with many overloads, but execution efficiency is improved

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

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.1.2 139 6/17/2025
1.1.1 141 6/17/2025
1.1.0 141 6/16/2025
1.0.0 87 5/30/2025