MakeInterface.Generator 1.0.4

dotnet add package MakeInterface.Generator --version 1.0.4
                    
NuGet\Install-Package MakeInterface.Generator -Version 1.0.4
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MakeInterface.Generator" Version="1.0.4">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MakeInterface.Generator" Version="1.0.4" />
                    
Directory.Packages.props
<PackageReference Include="MakeInterface.Generator">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MakeInterface.Generator --version 1.0.4
                    
#r "nuget: MakeInterface.Generator, 1.0.4"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=MakeInterface.Generator&version=1.0.4
                    
Install as a Cake Addin
#tool nuget:?package=MakeInterface.Generator&version=1.0.4
                    
Install as a Cake Tool

MakeInterface

Generate interfaces for your classes at compile time using a simple attribute. Generate interfaces for your classes at compile time using a simple attribute.

.NET

MakeInterface is a C# source generator that produces an I{ClassName} interface for any class marked with GenerateInterface. The generator analyses the public members of the class and writes the matching interface into your project's build output.

This is particularly useful when you simply need an interface to facilitate unit testing or dependency injection.

MakeInterface is a C# source generator that produces an I{ClassName} interface for any class marked with GenerateInterface. The generator analyses the public members of the class and writes the matching interface into your project's build output.

This is particularly useful when you simply need an interface to facilitate unit testing or dependency injection.

Usage

  1. Install the NuGet package (see Installation).
  2. Add the attribute to the class you want an interface for.
  3. Build your project. The interface will appear in your obj folder and be part of the compilation.
  4. Install the NuGet package (see Installation).
  5. Add the attribute to the class you want an interface for.
  6. Build your project. The interface will appear in your obj folder and be part of the compilation.
[GenerateInterface]
public class MyClass
{
        public string MyProperty { get; set; }
        public void MyMethod() { }
}

Need to omit a member? Use the Exclude property to provide a list of member names:

[GenerateInterface(Exclude = new[] { nameof(MyMethod) })]
public class MyClass
{
        public string MyProperty { get; set; }
        public void MyMethod() { }
}

Need to omit a member? Use the Exclude property to provide a list of member names:

[GenerateInterface(Exclude = new[] { nameof(MyMethod) })]
public class MyClass
{
        public string MyProperty { get; set; }
        public void MyMethod() { }
        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() { }
        public string MyProperty { get; set; }
        public void MyMethod() { }
}
[GenerateInterface]
public partial class MailService
{
    [InterfaceInclude]          // will appear in IMailService
    public Task SendAsync() { /*...*/ }

    public void Flush() { }   // will NOT appear because opt-in is active
}

If a class has no [InterfaceInclude] attributes, the generator keeps its original "include everything" behaviour.

When should I generate interfaces?

Generating interfaces works well when you only need an interface so the class can be mocked in unit tests or injected into other components. In that scenario your class is typically the single implementation and keeping the interface in sync manually becomes boilerplate. Let the generator do the work for you.

If you maintain many implementations of the same interface or the interface needs to diverge from the class surface, consider writing the interface yourself. Manually created interfaces give you more control over its shape and versioning.

Installation

Install the NuGet package MakeInterface:

dotnet add package MakeInterface.Generator

The GenerateInterface attribute is included in the package and will be available after the build without adding any extra references.

Versioning

This repository uses GitVersion in Continuous Deployment mode. Every build calculates a deterministic SemVer 2.0 version from the Git history. Local builds and CI therefore produce identical package and assembly versions.

License

MIT

Release process

  • Pushes to master publish prerelease packages to GitHub Packages using the version calculated by GitVersion.
  • Tagging the repository publishes the tagged version to NuGet.org when NUGET_API_KEY is configured.
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.4 282 6/10/2025
1.0.1 105 6/7/2025
1.0.0 112 6/7/2025
1.0.0-preview2 107 6/6/2025
1.0.0-preview1 107 6/6/2025
0.4.1 512 1/25/2024
0.4.0 233 1/17/2024
0.3.3 507 12/23/2022
0.3.2 329 12/22/2022
0.3.1 368 12/22/2022
0.3.0 336 12/21/2022
0.2.3 334 12/21/2022
0.2.2 337 12/21/2022
0.2.1 322 12/21/2022
0.2.0 334 12/19/2022
0.1.2 346 12/19/2022
0.1.1 357 12/19/2022
0.1.0 337 12/18/2022
0.0.3 380 12/16/2022
0.0.2 347 12/16/2022
0.0.1 339 12/16/2022