RSCG – MakeInterface.Generator
| name | MakeInterface.Generator |
| nuget | https://www.nuget.org/packages/MakeInterface.Generator/ |
| link | https://github.com/Frederik91/MakeInterface |
| author | Frederik |
Generating interface from class definition
This is how you can use MakeInterface.Generator .
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>
<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>
The code that you will use is
// 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());
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}";
}
}
The code that is generated is
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();
}
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/MakeInterface.Generator
Leave a Reply