Skip to content

Repository files navigation

Automatic Interface

A C# Source Generator to automatically create Interface from classes.

What does it do?

Not all .NET Interfaces are created equal. Some Interfaces are lovingly handcrafted, e.g. the public interface of your .NET package which is used by your customers. Other interfaces are far from lovingly crafted, they are birthed because you need an interface for testing or for the DI container. They are often implemented only once or twice: The class itself and a mock for testing. They are noise at best and often create lots of friction. Adding a new method / field? You have to edit the interface, too!. Change parameters? Edit the interface. Add documentation? Hopefully you add it to the interface, too!

This Source Generator aims to eliminate this cost by generating an interface from the class, without you needing to do anything. This interface will be generated on each subsequent build, eliminating the the friction.

Example

usingAutomaticInterfaceAttribute;usingSystem;namespaceAutomaticInterfaceExample{/// <summary>/// Class Documentation will be copied/// </summary>[GenerateAutomaticInterface]// you need to create an Attribute with exactly this name in your solution. You cannot reference Code from the Analyzer.classDemoClass:IDemoClass// You Interface will get the Name I+classname, here IDemoclass. // Generics, including constraints are allowed, too. E.g. MyClass<T> where T: class{/// <summary>/// Property Documentation will be copied/// </summary>publicstringHello{get;set;}// included, get and set are copied to the interface when publicpublicstringOnlyGet{get;}// included, get and set are copied to the interface when public/// <summary>/// Method Documentation will be copied/// </summary>publicstringAMethod(stringx,stringy)// included{returnBMethod(x,y);}privatestringBMethod(stringx,stringy)// ignored because not public{returnx+y;}publicstringCMethod<T,T1,T2,T3,T4>(string?x,stringy)// includedwhereT:classwhereT1:structwhereT3:DemoClasswhereT4:IDemoClass{return"Ok";}publicstaticstringStaticProperty=>"abc";// static property, ignoredpublicstaticstringStaticMethod()// static method, ignored{return"static"+DateTime.Now;}/// <summary>/// event Documentation will be copied/// </summary>publiceventEventHandlerShapeChanged;// includedprivateeventEventHandlerShapeChanged2;// ignored because not publicprivatereadonlyint[]arr=newint[100];publicintthis[intindex]// currently ignored{get=>arr[index];set=>arr[index]=value;}}}

This will create this interface:

#nullable enable
usingSystem.CodeDom.Compiler;usingAutomaticInterfaceAttribute;usingSystem;/// <summary>/// Result of the generator/// </summary>namespaceAutomaticInterfaceExample{/// <summary>/// Bla bla/// </summary>[GeneratedCode("AutomaticInterface","")]publicpartialinterfaceIDemoClass{/// <summary>/// Property Documentation will be copied/// </summary>stringHello{get;set;}stringOnlyGet{get;}/// <summary>/// Method Documentation will be copied/// </summary>stringAMethod(stringx,stringy);stringCMethod<T,T1,T2,T3,T4>(string?x,stringy)whereT:classwhereT1:structwhereT3:DemoClasswhereT4:IDemoClass;/// <summary>/// event Documentation will be copied/// </summary>eventSystem.EventHandlerShapeChanged;}}
#nullable restore

How to use it?

  1. Install the nuget: dotnet add package AutomaticInterface
  2. Create an Attribute with the Name [GenerateAutomaticInterface]. You can just copy the minimal code from this Repo (see the AutomaticInterfaceAttribute project). It's the easiest way to get that attribute because you cannot reference any code from the analyzer package.
  3. Let your class implement the interface, e.g. SomeClass: ISomeClass
  4. Build Solution, the Interface should now be available.

Any errors? Ping me at: christiian.sauer@codecentric.de

Troubleshooting

How can I see the Source code?

Newer Visual Studio Versions (2019+) can see the source code directly:

alt text

Alternatively, the Source Generator generates a log file - look out for a "logs" folder somewhere in bin/debug/... OR your temp folder /logs. The exact location is also reported on Diagnosticlevel Info.

I have an error

Please ping me via Github. Ideally create a Test for your problem - source generators are a pain to debug and tests solve that problem.

Contributors

Thanks to dnf for creating some great extensions. I use them partially in this Generator. Unfortunately due to problems referencing packages I cannot depend on his packages directly.

Run tests

Should be simply a build and run Tests

Changelog

1.5.0

  • Add support nullable context

1.4.0

  • Add support for overloaded methods.
  • Add support for optional parameters in method void test(string x = null) should now work.

About

.Net Core Source Generator for Automatic Interfaces

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages