RSCG – JinShil.MixinSourceGenerator
| name | JinShil.MixinSourceGenerator |
| nuget | https://www.nuget.org/packages/JinShil.MixinSourceGenerator/ |
| link | https://github.com/JinShil/JinShil.MixinSourceGenerator |
| author | Jin Shil |
Generate mixins of classes
This is how you can use JinShil.MixinSourceGenerator .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JinShil.MixinSourceGenerator" Version="1.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
The code that you will use is
using DemoMixin;
Person person = new Person { Name = "Andrei Ignat" };
person.LogName();
using JinShil.MixinSourceGenerator;
namespace DemoMixin;
[Mixin(typeof(LogData))]
partial class Person
{
public string Name { get; set; }=string.Empty;
public void LogName() => this.Log(Name);
}
namespace DemoMixin;
internal class LogData
{
public void Log(string data) => Console.WriteLine(data);
}
The code that is generated is
namespace JinShil.MixinSourceGenerator
{
/// <summary>
/// Specifies the type whose members are to be mixed in to a partial class or struct.
/// </summary>
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Struct, AllowMultiple = true)]
public class MixinAttribute : System.Attribute
{
public System.Type Type { get; }
public MixinAttribute(System.Type type)
{
Type = type;
}
}
}
namespace JinShil.MixinSourceGenerator
{
/// <summary>
/// Used to identify a member that should be ignored when mixing in members from other types.
/// </summary>
[System.AttributeUsage(System.AttributeTargets.Method | System.AttributeTargets.Property | System.AttributeTargets.Field | System.AttributeTargets.Event, AllowMultiple = false)]
public class MixinIgnoreAttribute : System.Attribute
{
public MixinIgnoreAttribute()
{ }
}
}
#nullable enable
namespace DemoMixin
{
partial class Person
{
public void Log(string data) => Console.WriteLine(data);
}
}
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/JinShil.MixinSourceGenerator