Roozie.AutoInterface by Alex Russak
Nuget / site data
Details
Info
Name: Roozie.AutoInterface
C# source generator to generate an interface for a class
Author: Alex Russak
NuGet: https://www.nuget.org/packages/Roozie.AutoInterface/
You can find more details at https://github.com/AlexRussak/Roozie.AutoInterface
Original Readme
Roozie.AutoInterface
What is it?
Roozie.AutoInterface is a C# source generator that generates an interface for a class. The generated interface contains the XML-doc comments, public properties, and public methods.
Why?
Interfaces are great for keeping your code loosely coupled and unit testable. But, they add some maintenance overhead. This source generator will keep your interfaces up to date.
How to use it?
Add the NuGet package to your project.
dotnet add package Roozie.AutoInterface --prerelease
Create a class where you want to generate an interface.
public class MyClass
{
public string MyProperty { get; set; }
public void MyMethod()
{
// Do something
}
}
- Add the
[AutoInterface]
attribute to the class. - An interface will be generated in the same namespace as the class.
You can now use the generated interface in your code.
If the class is partial
, the interface will be automatically implemented.
Check out the tests for examples.
Configuration
You can configure the generator in the [AutoInterface]
attribute. The following options are available:
Option | Default Value | Description |
---|---|---|
Name | "I" + Class name | Set the interface to whatever name you want. |
IncludeMethods | true | Set to false , the generator will automatically include methods in the interface. You can mark a method as included by adding the [AddToInterface] attribute. |
IncludeProperties | true | Same as IncludeMethods |
ImplementOnPartial | true | When true, the interface will be automatically implemented if the class is marked as partial. |
Contributing
Please open an issue if you find a bug or have a feature request. If you'd like to contribute, please open a pull request.
Kudos
Andrew Lock's Source Generator series is an excellent resource for learning all aspects of source generators.
About
Generating interfaces from project
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- Person.cs
This is the CSharp Project that references Roozie.AutoInterface
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Roozie.AutoInterface" Version="0.6.1" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
This is the use of Roozie.AutoInterface in Program.cs
using Roozie.AutoInterfaceDemo;
IPerson p = new Person();
p.FirstName = "Andrei";
p.LastName = "Ignat";
Console.WriteLine(p.FullName() );
This is the use of Roozie.AutoInterface in Person.cs
using Roozie.AutoInterface;
namespace Roozie.AutoInterfaceDemo;
[AutoInterface(IncludeMethods =true,IncludeProperties =true)]
public partial class Person
{
public int ID { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string FullName()
{
return FirstName + " " + LastName;
}
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- IPerson.g.cs
// <auto-generated>
// This file was generated by Roozie.AutoInterface v0.6.1.0
// </auto-generated>
using Roozie.AutoInterface;
namespace Roozie.AutoInterfaceDemo;
#nullable enable
public partial class Person : IPerson {}
public partial interface IPerson
{
int ID { get; set; }
string? FirstName { get; set; }
string? LastName { get; set; }
string FullName();
}
Usefull
Download Example (.NET C# )
Share Roozie.AutoInterface
https://ignatandrei.github.io/RSCG_Examples/v2/docs/Roozie.AutoInterface