Minerals.AutoInterfaces by Szymon Hałucha
Nuget / site data
Details
Info
Name: Minerals.AutoInterfaces
Package for automatic interface generation using incremental generator
Author: Szymon Hałucha
NuGet: https://www.nuget.org/packages/Minerals.AutoInterfaces/
You can find more details at https://github.com/SzymonHalucha/Minerals.AutoInterfaces
Source : https://github.com/SzymonHalucha/Minerals.AutoInterfaces
Original Readme
Minerals.AutoInterfaces
This NuGet package provides a functionality to automatically generate interfaces for C# classes with a single attribute. This simplifies the creation of interfaces for classes with clearly defined public members, without having to manually write interface code.
Features
- Automatic interface generation: Saves time and reduces the risk of errors when creating interfaces for classes.
- Support for generic methods and constraints: Allows for generating interfaces for complex classes with generic methods.
- Support for custom getters and setters: Generates interfaces for properties with custom getter and setter implementations.
- Customizable interface name: Allows you to name the interface according to naming conventions or user preferences.
- Compatible with .NET Standard 2.0 and C# 7.3+: Works on a wide range of platforms and development environments.
Installation
Add the Minerals.AutoInterfaces nuget package to your C# project using the following methods:
1. Project file definition
<PackageReference Include="Minerals.AutoInterfaces" Version="0.1.*" />
2. dotnet command
dotnet add package Minerals.AutoInterfaces
Usage
To use the package, add the [GenerateInterface]
attribute to the selected class.
namespace Examples
{
[Minerals.AutoInterfaces.GenerateInterface]
public class ExampleClass
{
public int Property1 { get; set; } = 1;
public int Property2 { get; private set; } = 2;
public int Property3
{
get { return _field1; }
set { _field1 = value; }
}
private int _field1 = 0;
public int Method1(int arg0, int arg1)
{
return arg0 + arg1;
}
public void Method2<T>(T arg0) where T : class, new()
{
return $"{arg0}";
}
protected void Method3() { }
}
}
The code above will generate the IExampleClass.g.cs
file with the IExampleClass
interface.
namespace Examples
{
[global::System.Runtime.CompilerServices.CompilerGenerated]
public interface IExampleClass
{
int Property1 { get; set; }
int Property2 { get; }
int Property3 { get; set; }
int Method1(int arg0, int arg1);
string Method2<T>(T arg0) where T : class, new();
}
}
Package supports custom interface names
namespace Examples
{
[Minerals.AutoInterfaces.GenerateInterface("ExampleInterface")]
public class ExampleClass
{
public int Property1 { get; protected set; } = 1;
}
}
The code above will generate the ExampleInterface.g.cs
file with the ExampleInterface
interface.
namespace Examples
{
[global::System.Runtime.CompilerServices.CompilerGenerated]
public interface ExampleInterface
{
int Property1 { get; }
}
}
Versioning
We use SemVer for versioning. For the versions available, see the branches on this repository.
Authors
- Szymon Hałucha - Maintainer
See also the list of contributors who participated in this project.
License
This project is licensed under the MIT License - see the LICENSE file for details.
About
Generating interface from class
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- Person.cs
This is the CSharp Project that references Minerals.AutoInterfaces
<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="Minerals.AutoInterfaces" Version="0.1.5" />
</ItemGroup>
</Project>
This is the use of Minerals.AutoInterfaces in Program.cs
// See https://aka.ms/new-console-template for more information
using Class2Interface;
Console.WriteLine("Hello, World!");
IPerson person=new Person();
person.FirstName="Andrei";
person.LastName="Ignat";
Console.WriteLine(person.FullName());
This is the use of Minerals.AutoInterfaces in Person.cs
namespace Class2Interface;
[Minerals.AutoInterfaces.GenerateInterface("IPerson")]
public partial class Person:IPerson
{
public int ID { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string Name
{
get
{
return $"{FirstName} {LastName}";
}
}
public string FullName()=>$"{FirstName} {LastName}";
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- GenerateInterfaceAttribute.g.cs
- IPerson.g.cs
// <auto-generated>
// This code was generated by a tool.
// Name: Minerals.AutoInterfaces
// Version: 0.1.5+54d6efe308ef06f041fc9b5d9285caeecef3e7c4
// </auto-generated>
#pragma warning disable CS9113
namespace Minerals.AutoInterfaces
{
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Runtime.CompilerServices.CompilerGenerated]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.AttributeUsage(global::System.AttributeTargets.Class | global::System.AttributeTargets.Struct, AllowMultiple = false, Inherited = false)]
public sealed class GenerateInterfaceAttribute : global::System.Attribute
{
public GenerateInterfaceAttribute(string customName = "")
{
}
}
}
#pragma warning restore CS9113
// <auto-generated>
// This code was generated by a tool.
// Name: Minerals.AutoInterfaces
// Version: 0.1.5+54d6efe308ef06f041fc9b5d9285caeecef3e7c4
// </auto-generated>
namespace Class2Interface
{
[global::System.Runtime.CompilerServices.CompilerGenerated]
public interface IPerson
{
int ID { get; set; }
string? FirstName { get; set; }
string? LastName { get; set; }
string Name { get; }
string FullName();
}
}
Usefull
Download Example (.NET C# )
Share Minerals.AutoInterfaces
https://ignatandrei.github.io/RSCG_Examples/v2/docs/Minerals.AutoInterfaces