name | InlineComposition |
nuget | https://www.nuget.org/packages/InlineComposition/ |
link | https://github.com/BlackWhiteYoshi/InlineComposition |
author | Black White Yoshi |
Mixin classes and interfaces together
This is how you can use InlineComposition .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net9.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <PropertyGroup> <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath> </PropertyGroup> <ItemGroup> <PackageReference Include="InlineComposition" Version="1.4.0"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference> </ItemGroup> </Project>
The code that you will use is
// See https://aka.ms/new-console-template for more information using MixinConsoleDemo; Console.WriteLine("Hello, World!"); Employee p = new Employee(); p.Name="Andrei Ignat"; p.Age = 55; p.Salary = 1000; p.Id = 1; Console.WriteLine($"Name: {p.Name}, Age: {p.Age}, Salary: {p.Salary}");
using InlineCompositionAttributes; namespace MixinConsoleDemo; [InlineBase] internal class Person { public string Name { get; set; } = string.Empty; public int Age { get; set; } }
using InlineCompositionAttributes; namespace MixinConsoleDemo; [Inline<Person,IId>] internal partial class Employee { public decimal Salary { get; set; } }
using InlineCompositionAttributes; namespace MixinConsoleDemo; [InlineBase] internal class IId { public int Id { get; set; } }
The code that is generated is
// <auto-generated/> #pragma warning disable #nullable enable annotations #if !INLINECOMPOSITION_EXCLUDE_ATTRIBUTES using System; namespace InlineCompositionAttributes; /// <summary> /// Generates a partial class/struct which includes all members listed in the typeParams. /// </summary> /// <typeparam name="T1">class/struct which members gets inlined.</typeparam> [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] [System.CodeDom.Compiler.GeneratedCodeAttribute("InlineComposition", "1.4.0")] internal sealed class InlineAttribute<T1> : Attribute { } /// <summary> /// Generates a partial class/struct which includes all members listed in the typeParams. /// </summary> /// <typeparam name="T1">class/struct which members gets inlined.</typeparam> /// <typeparam name="T2">class/struct which members gets inlined.</typeparam> [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] [System.CodeDom.Compiler.GeneratedCodeAttribute("InlineComposition", "1.4.0")] internal sealed class InlineAttribute<T1, T2> : Attribute { } /// <summary> /// Generates a partial class/struct which includes all members listed in the typeParams. /// </summary> /// <typeparam name="T1">class/struct which members gets inlined.</typeparam> /// <typeparam name="T2">class/struct which members gets inlined.</typeparam> /// <typeparam name="T3">class/struct which members gets inlined.</typeparam> [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] [System.CodeDom.Compiler.GeneratedCodeAttribute("InlineComposition", "1.4.0")] internal sealed class InlineAttribute<T1, T2, T3> : Attribute { } /// <summary> /// Generates a partial class/struct which includes all members listed in the typeParams. /// </summary> /// <typeparam name="T1">class/struct which members gets inlined.</typeparam> /// <typeparam name="T2">class/struct which members gets inlined.</typeparam> /// <typeparam name="T3">class/struct which members gets inlined.</typeparam> /// <typeparam name="T4">class/struct which members gets inlined.</typeparam> [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] [System.CodeDom.Compiler.GeneratedCodeAttribute("InlineComposition", "1.4.0")] internal sealed class InlineAttribute<T1, T2, T3, T4> : Attribute { } /// <summary> /// Generates a partial class/struct which includes all members listed in the typeParams. /// </summary> /// <typeparam name="T1">class/struct which members gets inlined.</typeparam> /// <typeparam name="T2">class/struct which members gets inlined.</typeparam> /// <typeparam name="T3">class/struct which members gets inlined.</typeparam> /// <typeparam name="T4">class/struct which members gets inlined.</typeparam> /// <typeparam name="T5">class/struct which members gets inlined.</typeparam> [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] [System.CodeDom.Compiler.GeneratedCodeAttribute("InlineComposition", "1.4.0")] internal sealed class InlineAttribute<T1, T2, T3, T4, T5> : Attribute { } /// <summary> /// Generates a partial class/struct which includes all members listed in the typeParams. /// </summary> /// <typeparam name="T1">class/struct which members gets inlined.</typeparam> /// <typeparam name="T2">class/struct which members gets inlined.</typeparam> /// <typeparam name="T3">class/struct which members gets inlined.</typeparam> /// <typeparam name="T4">class/struct which members gets inlined.</typeparam> /// <typeparam name="T5">class/struct which members gets inlined.</typeparam> /// <typeparam name="T6">class/struct which members gets inlined.</typeparam> [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] [System.CodeDom.Compiler.GeneratedCodeAttribute("InlineComposition", "1.4.0")] internal sealed class InlineAttribute<T1, T2, T3, T4, T5, T6> : Attribute { } /// <summary> /// Generates a partial class/struct which includes all members listed in the typeParams. /// </summary> /// <typeparam name="T1">class/struct which members gets inlined.</typeparam> /// <typeparam name="T2">class/struct which members gets inlined.</typeparam> /// <typeparam name="T3">class/struct which members gets inlined.</typeparam> /// <typeparam name="T4">class/struct which members gets inlined.</typeparam> /// <typeparam name="T5">class/struct which members gets inlined.</typeparam> /// <typeparam name="T6">class/struct which members gets inlined.</typeparam> /// <typeparam name="T7">class/struct which members gets inlined.</typeparam> [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] [System.CodeDom.Compiler.GeneratedCodeAttribute("InlineComposition", "1.4.0")] internal sealed class InlineAttribute<T1, T2, T3, T4, T5, T6, T7> : Attribute { } /// <summary> /// Generates a partial class/struct which includes all members listed in the typeParams. /// </summary> /// <typeparam name="T1">class/struct which members gets inlined.</typeparam> /// <typeparam name="T2">class/struct which members gets inlined.</typeparam> /// <typeparam name="T3">class/struct which members gets inlined.</typeparam> /// <typeparam name="T4">class/struct which members gets inlined.</typeparam> /// <typeparam name="T5">class/struct which members gets inlined.</typeparam> /// <typeparam name="T6">class/struct which members gets inlined.</typeparam> /// <typeparam name="T7">class/struct which members gets inlined.</typeparam> /// <typeparam name="T8">class/struct which members gets inlined.</typeparam> [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] [System.CodeDom.Compiler.GeneratedCodeAttribute("InlineComposition", "1.4.0")] internal sealed class InlineAttribute<T1, T2, T3, T4, T5, T6, T7, T8> : Attribute { } /// <summary> /// Generates a partial class/struct which includes all members listed in the typeParams. /// </summary> /// <typeparam name="T1">class/struct which members gets inlined.</typeparam> /// <typeparam name="T2">class/struct which members gets inlined.</typeparam> /// <typeparam name="T3">class/struct which members gets inlined.</typeparam> /// <typeparam name="T4">class/struct which members gets inlined.</typeparam> /// <typeparam name="T5">class/struct which members gets inlined.</typeparam> /// <typeparam name="T6">class/struct which members gets inlined.</typeparam> /// <typeparam name="T7">class/struct which members gets inlined.</typeparam> /// <typeparam name="T8">class/struct which members gets inlined.</typeparam> /// <typeparam name="T9">class/struct which members gets inlined.</typeparam> [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] [System.CodeDom.Compiler.GeneratedCodeAttribute("InlineComposition", "1.4.0")] internal sealed class InlineAttribute<T1, T2, T3, T4, T5, T6, T7, T8, T9> : Attribute { } /// <summary> /// Generates a partial class/struct which includes all members listed in the typeParams. /// </summary> /// <typeparam name="T1">class/struct which members gets inlined.</typeparam> /// <typeparam name="T2">class/struct which members gets inlined.</typeparam> /// <typeparam name="T3">class/struct which members gets inlined.</typeparam> /// <typeparam name="T4">class/struct which members gets inlined.</typeparam> /// <typeparam name="T5">class/struct which members gets inlined.</typeparam> /// <typeparam name="T6">class/struct which members gets inlined.</typeparam> /// <typeparam name="T7">class/struct which members gets inlined.</typeparam> /// <typeparam name="T8">class/struct which members gets inlined.</typeparam> /// <typeparam name="T9">class/struct which members gets inlined.</typeparam> /// <typeparam name="T10">class/struct which members gets inlined.</typeparam> [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] [System.CodeDom.Compiler.GeneratedCodeAttribute("InlineComposition", "1.4.0")] internal sealed class InlineAttribute<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> : Attribute { } /// <summary> /// Generates a partial class/struct which includes all members listed in the typeParams. /// </summary> /// <typeparam name="T1">class/struct which members gets inlined.</typeparam> /// <typeparam name="T2">class/struct which members gets inlined.</typeparam> /// <typeparam name="T3">class/struct which members gets inlined.</typeparam> /// <typeparam name="T4">class/struct which members gets inlined.</typeparam> /// <typeparam name="T5">class/struct which members gets inlined.</typeparam> /// <typeparam name="T6">class/struct which members gets inlined.</typeparam> /// <typeparam name="T7">class/struct which members gets inlined.</typeparam> /// <typeparam name="T8">class/struct which members gets inlined.</typeparam> /// <typeparam name="T9">class/struct which members gets inlined.</typeparam> /// <typeparam name="T10">class/struct which members gets inlined.</typeparam> /// <typeparam name="T11">class/struct which members gets inlined.</typeparam> [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] [System.CodeDom.Compiler.GeneratedCodeAttribute("InlineComposition", "1.4.0")] internal sealed class InlineAttribute<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> : Attribute { } /// <summary> /// Generates a partial class/struct which includes all members listed in the typeParams. /// </summary> /// <typeparam name="T1">class/struct which members gets inlined.</typeparam> /// <typeparam name="T2">class/struct which members gets inlined.</typeparam> /// <typeparam name="T3">class/struct which members gets inlined.</typeparam> /// <typeparam name="T4">class/struct which members gets inlined.</typeparam> /// <typeparam name="T5">class/struct which members gets inlined.</typeparam> /// <typeparam name="T6">class/struct which members gets inlined.</typeparam> /// <typeparam name="T7">class/struct which members gets inlined.</typeparam> /// <typeparam name="T8">class/struct which members gets inlined.</typeparam> /// <typeparam name="T9">class/struct which members gets inlined.</typeparam> /// <typeparam name="T10">class/struct which members gets inlined.</typeparam> /// <typeparam name="T11">class/struct which members gets inlined.</typeparam> /// <typeparam name="T12">class/struct which members gets inlined.</typeparam> [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] [System.CodeDom.Compiler.GeneratedCodeAttribute("InlineComposition", "1.4.0")] internal sealed class InlineAttribute<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> : Attribute { } #endif
// <auto-generated/> #pragma warning disable #nullable enable annotations #if !INLINECOMPOSITION_EXCLUDE_ATTRIBUTES using System; namespace InlineCompositionAttributes; /// <summary> /// <para>Marks this class/struct as inlineable, so it can be listed in a <see cref="InlineAttribute{T1}"/> Attribute.</para> /// </summary> [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] [System.CodeDom.Compiler.GeneratedCodeAttribute("InlineComposition", "1.4.0")] internal sealed class InlineBaseAttribute : Attribute { /// <summary> /// <para>If set all occurrences of the type of the inlineBase class/struct get replaced with the type of the inlining class/struct.</para> /// <para>e.g. if "Example" inlines "Test" with this option enabled, all occurrences of type "Test" inside class/struct "Test" will be mapped to "Example".</para> /// </summary> public bool MapBaseType { get; init; } /// <summary> /// If set the generator ignores the inherited class and implemented interfaces of this type. /// </summary> public bool IgnoreInheritenceAndImplements { get; init; } /// <summary> /// If set attributes of this class/struct are inlined as well.<br /> /// The attribute [InlineBase] itself is ignored. /// </summary> public bool InlineAttributes { get; init; } } #endif
// <auto-generated/> #pragma warning disable #nullable enable annotations #if !INLINECOMPOSITION_EXCLUDE_ATTRIBUTES using System; namespace InlineCompositionAttributes; /// <summary> /// The Method under this attribute will be inlined in the constructor. /// </summary> [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor)] [System.CodeDom.Compiler.GeneratedCodeAttribute("InlineComposition", "1.4.0")] internal sealed class InlineConstructorAttribute : Attribute { /// <summary> /// <para>Modifiers e.g. "public", "protected", "private"</para> /// <para>If null, the method modifiers will be taken.</para> /// </summary> public string? Modifiers { get; init; } /// <summary> /// Indicates whether this method gets inlined before the other constructors or after. /// </summary> public bool First { get; init; } } #endif
// <auto-generated/> #pragma warning disable #nullable enable annotations #if !INLINECOMPOSITION_EXCLUDE_ATTRIBUTES using System; namespace InlineCompositionAttributes; /// <summary> /// The Method under this attribute will be inlined in the finalizer. /// </summary> [AttributeUsage(AttributeTargets.Method)] [System.CodeDom.Compiler.GeneratedCodeAttribute("InlineComposition", "1.4.0")] internal sealed class InlineFinalizerAttribute : Attribute { /// <summary> /// Indicates whether this method gets inlined before the other finalizers or after. /// </summary> public bool First { get; init; } } #endif
// <auto-generated/> #pragma warning disable #nullable enable annotations #if !INLINECOMPOSITION_EXCLUDE_ATTRIBUTES using System; namespace InlineCompositionAttributes; /// <summary> /// The Method under this attribute will be inlined in the method given by <see cref="MethodName"/>. /// </summary> [AttributeUsage(AttributeTargets.Method)] [System.CodeDom.Compiler.GeneratedCodeAttribute("InlineComposition", "1.4.0")] internal sealed class InlineMethodAttribute : Attribute { /// <summary> /// The method name as string literal. /// </summary> public required string MethodName { get; init; } /// <summary> /// <para>Modifiers e.g. "public static extern", "protected abstract"</para> /// <para>If null, the method modifiers will be taken.</para> /// </summary> public string? Modifiers { get; init; } /// <summary> /// Indicates whether this method gets inlined before the other methods or after. /// </summary> public bool First { get; init; } } #endif
// <auto-generated/> #pragma warning disable #nullable enable annotations using InlineCompositionAttributes; namespace MixinConsoleDemo; internal partial class Employee { public string Name { get; set; } = string.Empty; public int Age { get; set; } public int Id { get; set; } }
// <auto-generated/> #pragma warning disable #nullable enable annotations #if !INLINECOMPOSITION_EXCLUDE_ATTRIBUTES using System; namespace InlineCompositionAttributes; /// <summary> /// <para>Only usefule in a class/struct with a <see cref="InlineBaseAttribute"/>.</para> /// <para>Skips/Ignores this member.</para> /// </summary> [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Event | AttributeTargets.Method)] [System.CodeDom.Compiler.GeneratedCodeAttribute("InlineComposition", "1.4.0")] internal sealed class NoInlineAttribute : Attribute { } #endif
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/InlineComposition