MBW.Generators.OverloadGenerator
0.3.0
dotnet add package MBW.Generators.OverloadGenerator --version 0.3.0
NuGet\Install-Package MBW.Generators.OverloadGenerator -Version 0.3.0
<PackageReference Include="MBW.Generators.OverloadGenerator" Version="0.3.0" />
<PackageVersion Include="MBW.Generators.OverloadGenerator" Version="0.3.0" />
<PackageReference Include="MBW.Generators.OverloadGenerator" />
paket add MBW.Generators.OverloadGenerator --version 0.3.0
#r "nuget: MBW.Generators.OverloadGenerator, 0.3.0"
#:package MBW.Generators.OverloadGenerator@0.3.0
#addin nuget:?package=MBW.Generators.OverloadGenerator&version=0.3.0
#tool nuget:?package=MBW.Generators.OverloadGenerator&version=0.3.0
MBW.Generators.OverloadGenerator
About
Generates overloads for methods based on declarative attributes. Apply attributes to a method or containing type and the generator emits additional overloads that either transform a parameter or supply a default value. This is useful when you want a terse public API but still allow callers to pass richer types.
Imagine an API that accepts a string kind and an optional retry flag. Callers might prefer passing an OrderKind
enum instead of remembering the string representation, and most will keep the retry flag at its default.
Quick Start
- Install the
MBW.Generators.OverloadGenerator
package. - Decorate your partial class or individual methods with
[TransformOverload]
or[DefaultOverload]
. - The generator emits overloads for decorated methods, preserving visibility, parameter names, generic constraints, and default parameter values while keeping extension methods as extensions.
The package includes both the attributes and the source generator.
Example
using MBW.Generators.OverloadGenerator;
using MBW.Generators.OverloadGenerator.Attributes;
public enum OrderKind { Standard, Express }
[TransformOverload("kind", typeof(OrderKind), "{value}.ToString(\"G\", System.Globalization.CultureInfo.InvariantCulture)")]
[DefaultOverload("retry", "true")]
public partial class Orders
{
public void Create(string kind, bool retry) { }
}
Generates
// <auto-generated/>
partial class Orders
{
public void Create(global::OrderKind kind, bool retry)
=> Create(kind.ToString("G", System.Globalization.CultureInfo.InvariantCulture), retry);
public void Create(string kind)
=> Create(kind, true);
}
Features
- Replace parameters with strongly typed alternatives or supply defaults.
- Apply attributes at type or method level for flexible targeting.
- Supports extension methods and generic types, preserving generic constraints and default parameter values.
- Uses incremental generators for performance.
- Emits diagnostics to help guide usage, including signature collisions, missing parameters, invalid
accept
types, missing{value}
tokens, and attempts to remove thethis
receiver of an extension method. - Preserves original method visibility and parameter names.
- Generates interface members with default bodies where applicable.
Attributes
- TransformOverloadAttribute – apply to a type or method. Replaces a string parameter with a strongly typed version. Parameters:
parameter
– the name of the parameter to replace.accept
– the type callers can supply. Unsupported types produce a diagnostic.transform
– an expression where{value}
is substituted with the parameter.newTypeNullability
– optionally mark the new type as nullable.methodNameMatch
/methodNameReplace
– regex pair for renaming generated methods.
- DefaultOverloadAttribute – apply to a type or method. Emits an overload that supplies a default expression when the specified parameter is omitted. Parameters:
parameter
– the name of the parameter to default.defaultExpression
– expression inserted for the missing argument.methodNameMatch
/methodNameReplace
– regex pair for renaming generated methods.
Removing the this
receiver of an extension method is not supported and will trigger a diagnostic.
Both attributes may appear multiple times and are not inherited.
More information
This project is provided as-is without support. Further usage examples can be found in the tests.
Product | Versions 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. |
-
.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 |
---|---|---|
0.3.0 | 154 | 9/1/2025 |