| name | Tenekon.MethodOverloads.SourceGenerator |
| nuget | https://www.nuget.org/packages/Tenekon.MethodOverloads.SourceGenerator/ |
| link | https://github.com/tenekon/Tenekon.MethodOverloads.SourceGenerator |
| author | Tenekon |
generate overloads of the same method
This is how you can use Tenekon.MethodOverloads.SourceGenerator .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Tenekon.MethodOverloads.SourceGenerator" Version="0.0.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
The code that you will use is
namespace overloadMethod;
public class Person
{
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
[Tenekon.MethodOverloads.GenerateOverloads(Begin = nameof(MiddleName))]
public string FullName(string MiddleName, bool ToLowerCase)
{
var fullName = $"{FirstName} {MiddleName} {LastName}";
return ToLowerCase ? fullName.ToLower() : fullName;
}
}
using overloadMethod; Person p = new(); p.FirstName= "Andrei"; p.LastName = "Ignat"; Console.WriteLine(p.FullName());
The code that is generated is
namespace Microsoft.CodeAnalysis; internal sealed partial class EmbeddedAttribute : global::System.Attribute;
#nullable enable
namespace Tenekon.MethodOverloads;
[global::Microsoft.CodeAnalysis.Embedded]
[global::System.AttributeUsage(
global::System.AttributeTargets.Class
| global::System.AttributeTargets.Struct
| global::System.AttributeTargets.Interface,
AllowMultiple = true)]
internal sealed class GenerateMethodOverloadsAttribute : global::System.Attribute
{
public global::System.Type[]? Matchers { get; set; }
}
#nullable enable
namespace Tenekon.MethodOverloads;
[global::Microsoft.CodeAnalysis.Embedded]
[global::System.AttributeUsage(global::System.AttributeTargets.Method, AllowMultiple = true)]
internal sealed class GenerateOverloadsAttribute : global::System.Attribute
{
public GenerateOverloadsAttribute()
{
}
public GenerateOverloadsAttribute(string beginEnd)
{
Begin = beginEnd;
End = beginEnd;
}
/// <summary>
/// All parameters beginning from <see cref="Begin"/> (inclusive) are considered for optional or required.
/// </summary>
public string? Begin { get; set; }
/// <summary>
/// All parameters after <see cref="BeginExclusive"/> are considered for optional or required.
/// </summary>
public string? BeginExclusive { get; set; }
/// <summary>
/// All parameters before <see cref="EndExclusive"/> are considered for optional or required.
/// </summary>
public string? EndExclusive { get; set; }
/// <summary>
/// All parameters until <see cref="End"/> (inclusive) are considered for optional or required.
/// </summary>
public string? End { get; set; }
/// <summary>
/// Parameters listed here are always omitted from generated overloads within the resolved window.
/// </summary>
public string[]? ExcludeAny { get; set; }
public global::System.Type[]? Matchers { get; set; }
}
#nullable enable
namespace Tenekon.MethodOverloads;
[global::Microsoft.CodeAnalysis.Embedded]
[global::System.AttributeUsage(global::System.AttributeTargets.Class, AllowMultiple = true)]
internal sealed class MatcherUsageAttribute : global::System.Attribute
{
public MatcherUsageAttribute(string methodName)
{
}
}
// <auto-generated/>
#nullable enable
namespace overloadMethod;
public static class MethodOverloads
{
public static string FullName(this global::overloadMethod.Person source, bool ToLowerCase) => source.FullName(default(string), ToLowerCase);
public static string FullName(this global::overloadMethod.Person source, string MiddleName) => source.FullName(MiddleName, default(bool));
public static string FullName(this global::overloadMethod.Person source) => source.FullName(default(string), default(bool));
}
#nullable enable
namespace Tenekon.MethodOverloads;
[global::Microsoft.CodeAnalysis.Embedded]
internal enum RangeAnchorMatchMode
{
TypeOnly,
TypeAndName
}
[global::Microsoft.CodeAnalysis.Embedded]
internal enum OverloadSubsequenceStrategy
{
PrefixOnly,
UniqueBySignature
}
[global::Microsoft.CodeAnalysis.Embedded]
internal enum OverloadVisibility
{
MatchTarget,
Public,
Internal,
Private
}
[global::Microsoft.CodeAnalysis.Embedded]
[global::System.AttributeUsage(
global::System.AttributeTargets.Class | global::System.AttributeTargets.Struct
| global::System.AttributeTargets.Interface | global::System.AttributeTargets.Method)]
internal sealed class OverloadGenerationOptionsAttribute : global::System.Attribute
{
public RangeAnchorMatchMode RangeAnchorMatchMode { get; set; }
public OverloadSubsequenceStrategy SubsequenceStrategy { get; set; }
public OverloadVisibility OverloadVisibility { get; set; }
public global::System.Type? BucketType { get; set; }
}
#nullable enable
namespace Tenekon.MethodOverloads;
[global::Microsoft.CodeAnalysis.Embedded]
[global::System.AttributeUsage(
global::System.AttributeTargets.Method
| global::System.AttributeTargets.Class
| global::System.AttributeTargets.Interface,
AllowMultiple = true)]
internal sealed class SupplyParameterTypeAttribute : global::System.Attribute
{
public SupplyParameterTypeAttribute(string typeParameterName, global::System.Type suppliedType)
{
TypeParameterName = typeParameterName;
SuppliedType = suppliedType;
}
public string TypeParameterName { get; }
public global::System.Type SuppliedType { get; }
public object? Group { get; set; }
}
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/Tenekon.MethodOverloads.SourceGenerator