Hsu.Sg.FluentMember by Net Hsu
Nuget / site data
Details
Info
Name: Hsu.Sg.FluentMember
a fluent member source generator.
Author: Net Hsu
NuGet: https://www.nuget.org/packages/Hsu.Sg.FluentMember/
You can find more details at https://github.com/hsu-net/source-generators
Original Readme
Hsu.Sg
.NET source generators
Package Version
Name | Source | Stable | Preview |
---|---|---|---|
Hsu.Sg.Sync | Nuget | ||
Hsu.Sg.Sync | MyGet | ||
Hsu.Sg.Proxy | Nuget | ||
Hsu.Sg.Proxy | MyGet | ||
Hsu.Sg.FluentMember | Nuget | ||
Hsu.Sg.FluentMember | MyGet |
Package Features
Hsu.Sg.Sync
Generate a synchronous method from an asynchronous method.
Usages see README
Hsu.Sg.Proxy
Generate a proxy object from a struct
or class
or interface
.
Usages see README
Hsu.Sg.FluentMember
Generate a fluent method from a struct
or class
.
Usages see README
References
License
About
Adding builder pattern to classes
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- Person.cs
This is the CSharp Project that references Hsu.Sg.FluentMember
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Hsu.Sg.FluentMember" Version="2024.101.8-rc175707">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
This is the use of Hsu.Sg.FluentMember in Program.cs
using Builder;
var pOld = new Person();
pOld= pOld.WithFirstName("Andrei").WithLastName("Ignat").WithMiddleName("G");
System.Console.WriteLine(pOld.FullName());
This is the use of Hsu.Sg.FluentMember in Person.cs
namespace Builder;
[Hsu.Sg.FluentMember.FluentMember]
public partial class Person
{
public string FirstName { get; init; }
public string? MiddleName { get; init; }
public string LastName { get; init; }
public string FullName()
{
return FirstName + " " + MiddleName + " "+LastName;
}
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- Hsu.Sg.FluentMember.gen.cs
// <auto-generated/>
using System;
namespace Hsu.Sg.FluentMember
{
/// <summary>
/// The flag to generate member set method.
/// </summary>
[AttributeUsage(
AttributeTargets.Struct |
AttributeTargets.Class,
AllowMultiple = false,
Inherited = false)]
internal sealed class FluentMemberAttribute : Attribute
{
/// <summary>
/// The public member are generated.
/// </summary>
public bool Public { get; set; } = true;
/// <summary>
/// The internal member are generated.
/// </summary>
public bool Internal { get; set; }
/// <summary>
/// The private member are generated.
/// </summary>
public bool Private { get; set; }
/// <summary>
/// Only [FluentMemberGen] member are generated.
/// </summary>
public bool Only { get; set; }
/// <summary>
/// The prefix of member name.
/// </summary>
/// <remarks>default is `With`</remarks>
public string Prefix { get; set; } = string.Empty;
}
[AttributeUsage(AttributeTargets.Field |
AttributeTargets.Property |
AttributeTargets.Event,
AllowMultiple = false,
Inherited = false)]
internal sealed class FluentMemberGenAttribute : Attribute
{
/// <summary>
/// Ignore member.
/// </summary>
public bool Ignore { get; set; }
/// <summary>
/// The specific name of member.
/// </summary>
public string Identifier { get; set; } = string.Empty;
/// <summary>
/// The prefix of member name.
/// </summary>
/// <remarks>default is `With`</remarks>
public string Prefix { get; set; } = string.Empty;
/// <summary>
/// The modifier of member
/// </summary>
/// <remarks>default is <see cref="Accessibility.Inherit"/></remarks>
public Accessibility Modifier { get; set; } = Accessibility.Inherit;
}
/// <summary>
/// The accessibility for fluent member set method.
/// </summary>
//[System.DefaultValue(Inherit)]
internal enum Accessibility
{
/// <summary>
/// Inherit from the member.
/// </summary>
Inherit,
/// <summary>
/// Is public access.
/// </summary>
Public,
/// <summary>
/// Is internal access.
/// </summary>
Internal,
/// <summary>
/// Is protected access.
/// </summary>
Protected,
/// <summary>
/// Is private access.
/// </summary>
Private
}
/// <summary>
/// The event assignment
/// </summary>
//[System.DefaultValue(Add)]
public enum EventAssignable
{
/// <summary>
/// To add the event
/// </summary>
Add,
/// <summary>
/// To remove the event
/// </summary>
Remove,
/// <summary>
/// To set the event
/// </summary>
Assign
}
}
Usefull
Download Example (.NET C# )
Share Hsu.Sg.FluentMember
https://ignatandrei.github.io/RSCG_Examples/v2/docs/Hsu.Sg.FluentMember