RSCG – NetAutomaticInterface
| name | NetAutomaticInterface |
| nuget | https://www.nuget.org/packages/AutomaticInterface/ |
| link | https://github.com/codecentric/net_automatic_interface |
| author | codecentric AG |
GEnerating interface from class
This is how you can use NetAutomaticInterface .
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>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutomaticInterface" Version="2.1.0" OutputItemType="Analyzer" >
<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 Class2Interface; Console.WriteLine("Hello,World!"); IPerson person=new Person(); person.FirstName="Andrei"; person.LastName="Ignat"; Console.WriteLine(person.FullName());
[AttributeUsage(AttributeTargets.Class)]
public class GenerateAutomaticInterfaceAttribute : Attribute
{
public GenerateAutomaticInterfaceAttribute(string namespaceName = "") { }
}
namespace Class2Interface;
[GenerateAutomaticInterface("Class2Interface")]
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()=>$"{FirstName} {LastName}";
}
The code that is generated is
//--------------------------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
//--------------------------------------------------------------------------------------------------
using System.CodeDom.Compiler;
namespace Class2Interface
{
[GeneratedCode("AutomaticInterface","")]
public partial interface IPerson
{
/// <inheritdoc />
int ID { get; set; }
/// <inheritdoc />
string FirstName { get; set; }
/// <inheritdoc />
string LastName { get; set; }
/// <inheritdoc />
string Name { get; }
/// <inheritdoc />
string FullName();
}
}
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/NetAutomaticInterface
Leave a Reply