RSCG – AutoInterface
name | AutoInterface |
nuget | https://www.nuget.org/packages/AutoInterface/ |
link | https://github.com/BlackWhiteYoshi/AutoInterface |
author | Black White Yoshi |
generating interface from a class
This is how you can use AutoInterface .
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="AutoInterface" Version="2.4.0"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference> </ItemGroup> </Project>
The code that you will use is
using NullInterface; Console.WriteLine("Hello, World!"); Console.WriteLine("Hello, World!"); IDepartment department = new Department(); department.Name = "IT"; IEmployee employee = new Employee(); employee.FirstName = "Andrei"; employee.Department = department; Console.WriteLine(employee.FirstName); Console.WriteLine(employee.Department.Name); Console.WriteLine(employee.GetFullNameAndDepartment(" - "));
using AutoInterfaceAttributes; namespace NullInterface; [AutoInterface] public class Department : IDepartment { public string Name { get; set; } = string.Empty; }
using AutoInterfaceAttributes; namespace NullInterface; [AutoInterface] public class Employee: IEmployee { public string FirstName { get; set; } = string.Empty; public string LastName { get; set; } = string.Empty; public IDepartment Department { get; set; } public string GetFullName()=> $"{FirstName} {LastName}"; public string GetFullNameAndDepartment(string separator)=> $"{GetFullName()}{separator}{ Department?.Name}"; }
The code that is generated is
// <auto-generated/> #pragma warning disable #nullable enable annotations #if !AUTOINTERFACE_EXCLUDE_ATTRIBUTES using System; namespace AutoInterfaceAttributes; /// <summary> /// Generates an interface for the decorated class/struct. /// </summary> [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = true)] [System.CodeDom.Compiler.GeneratedCodeAttribute("AutoInterface", "2.4.0")] internal sealed class AutoInterfaceAttribute : Attribute { /// <summary> /// <para>The name of the generated interface.</para> /// <para>Default is "I{ClassName}"</para> /// </summary> public string Name { get; init; } /// <summary> /// <para>The modifier(s) for the interface.</para> /// <para>Deault is "public partial"</para> /// </summary> public string Modifier { get; init; } /// <summary> /// <para>The namespace declaration for the interface.</para> /// <para>If empty string, no namespace directive will be used (global namespace).<br /> /// Default (if not present) it will be mapped to the same namespace as the namespace of the class/struct.</para> /// </summary> public string Namespace { get; init; } /// <summary> /// <para>interface inheritance: Name(s) of interfaces this interface will inherit.</para> /// <para>Default is Array.Empty</para> /// </summary> public Type[] Inheritance { get; init; } /// <summary> /// <para> /// The Classes, structs or interfaces containing the generated interface.<br /> /// e.g. ["public sealed partial class Example"] will wrap the interface with that expression. /// </para> /// <para>Default is Array.Empty</para> /// </summary> public string[] Nested { get; init; } /// <summary> /// <para>If enabled, static members get accepted and are generating "static abstract" members.</para> /// <para>Default is false</para> /// </summary> public bool StaticMembers { get; init; } } #endif
// <auto-generated/> #pragma warning disable #nullable enable annotations #if !AUTOINTERFACE_EXCLUDE_ATTRIBUTES using System; namespace AutoInterfaceAttributes; /// <summary> /// Adds a "internal" access modifier to the interface member. /// </summary> [AttributeUsage(AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Event)] [System.CodeDom.Compiler.GeneratedCodeAttribute("AutoInterface", "2.4.0")] internal sealed class AutoInterfaceVisibilityInternal : Attribute { } #endif
// <auto-generated/> #pragma warning disable #nullable enable annotations #if !AUTOINTERFACE_EXCLUDE_ATTRIBUTES using System; namespace AutoInterfaceAttributes; /// <summary> /// Adds a "private protected" access modifier to the interface member. /// </summary> [AttributeUsage(AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Event)] [System.CodeDom.Compiler.GeneratedCodeAttribute("AutoInterface", "2.4.0")] internal sealed class AutoInterfaceVisibilityPrivateProtected : Attribute { } #endif
// <auto-generated/> #pragma warning disable #nullable enable annotations #if !AUTOINTERFACE_EXCLUDE_ATTRIBUTES using System; namespace AutoInterfaceAttributes; /// <summary> /// Adds a "protected" access modifier to the interface member. /// </summary> [AttributeUsage(AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Event)] [System.CodeDom.Compiler.GeneratedCodeAttribute("AutoInterface", "2.4.0")] internal sealed class AutoInterfaceVisibilityProtected : Attribute { } #endif
// <auto-generated/> #pragma warning disable #nullable enable annotations #if !AUTOINTERFACE_EXCLUDE_ATTRIBUTES using System; namespace AutoInterfaceAttributes; /// <summary> /// Adds a "protected internal" access modifier to the interface member. /// </summary> [AttributeUsage(AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Event)] [System.CodeDom.Compiler.GeneratedCodeAttribute("AutoInterface", "2.4.0")] internal sealed class AutoInterfaceVisibilityProtectedInternal : Attribute { } #endif
// <auto-generated/> #pragma warning disable #nullable enable annotations #if !AUTOINTERFACE_EXCLUDE_ATTRIBUTES using System; namespace AutoInterfaceAttributes; /// <summary> /// Adds a "public" access modifier to the interface member. /// </summary> [AttributeUsage(AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Event)] [System.CodeDom.Compiler.GeneratedCodeAttribute("AutoInterface", "2.4.0")] internal sealed class AutoInterfaceVisibilityPublic : Attribute { } #endif
// <auto-generated/> #pragma warning disable #nullable enable annotations using AutoInterfaceAttributes; namespace NullInterface; public partial interface IDepartment { string Name { get; set; } }
// <auto-generated/> #pragma warning disable #nullable enable annotations namespace NullInterface; public partial interface IDepartment { string Name { get; set; } }
// <auto-generated/> #pragma warning disable #nullable enable annotations using AutoInterfaceAttributes; namespace NullInterface; public partial interface IEmployee { string FirstName { get; set; } string LastName { get; set; } IDepartment Department { get; set; } string GetFullName(); string GetFullNameAndDepartment(string separator); }
// <auto-generated/> #pragma warning disable #nullable enable annotations #if !AUTOINTERFACE_EXCLUDE_ATTRIBUTES using System; namespace AutoInterfaceAttributes; /// <summary> /// The decorated member will be Ignored by the generator. /// </summary> [AttributeUsage(AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Event)] [System.CodeDom.Compiler.GeneratedCodeAttribute("AutoInterface", "2.4.0")] internal sealed class IgnoreAutoInterfaceAttribute : Attribute { } #endif
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/AutoInterface