MakeInterface.Generator by Frederik
Nuget / site data
Details
Info
Name: MakeInterface.Generator
Generates interfaces for classes
Author: Frederik
NuGet: https://www.nuget.org/packages/MakeInterface.Generator/
You can find more details at https://github.com/Frederik91/MakeInterface
Original Readme
MakeInterface
Creates an interface of a class using source generator
Usage
Add the attribute to the class you want to generate the interface for
[GenerateInterface]
public class MyClass
{
public string MyProperty { get; set; }
public void MyMethod() { }
}
The generated interface will then be generated as IMyClass.g.cs
public interface IMyClass
{
string MyProperty { get; set; }
void MyMethod();
}
You can then implement the interface in your class
public class MyClass : IMyClass
{
public string MyProperty { get; set; }
public void MyMethod() { }
}
Installation
Install the NuGet package MakeInterface
You can either create the attribute yourself or use the one provided in the package MakeInterface.Contracts
License
MIT
About
Generating interface from class definition
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- Person.cs
This is the CSharp Project that references MakeInterface.Generator
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MakeInterface.Contracts" Version="0.4.0" />
<PackageReference Include="MakeInterface.Generator" Version="0.4.0" OutputItemType="Analyzer" >
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
This is the use of MakeInterface.Generator 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 MakeInterface.Generator in Person.cs
using MakeInterface.Contracts.Attributes;
namespace Class2Interface;
[GenerateInterface]
internal 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()
{
return $"{FirstName} {LastName}";
}
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- IPerson.g.cs
using MakeInterface.Contracts.Attributes;
// <auto-generated/>
#pragma warning disable
#nullable enable
namespace Class2Interface;
public partial 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 MakeInterface.Generator
https://ignatandrei.github.io/RSCG_Examples/v2/docs/MakeInterface.Generator