Skip to content

Repository files navigation

AutoInterface

AutoInterface is a source generator that generates an interface based on your class/struct.
Basically, you write your class and get the corresponding interface for free.

usingAutoInterfaceAttributes;[AutoInterface]publicclassExample:IExample{publicintNumber{get;privateset;}publicExample(){ResetNumber();}/// <summary>/// some method description/// </summary>publicintAddToNumber(intincrease){Number+=increase;returnNumber;}privatevoidResetNumber()=>Number=0;}
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
usingAutoInterfaceAttributes;publicpartialinterfaceIExample{intNumber{get;}/// <summary>/// some method description/// </summary>intAddToNumber(intincrease);}

AutoInterface supports:



Examples

AutoInterfaceAttribute on struct

usingAutoInterfaceAttributes;[AutoInterface]publicstructPoint{publicintX{get;privateset;}publicintY{get;privateset;}publicPoint(intx,inty)=>(X,Y)=(x,y);}
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
usingAutoInterfaceAttributes;publicpartialinterfaceIPoint{intX{get;}intY{get;}}



AutoInterfaceAttribute with all kinds of members

usingAutoInterfaceAttributes;[AutoInterface]publicsealedclassFullExample{publicvoidSomeMethod(){}publicintSomeProperty{get;init;}publicintthis[inti]=>i;publiceventAction?someEvent;publiceventActionSomeEvent{add{}remove{}}}
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
usingAutoInterfaceAttributes;publicpartialinterfaceIFullExample{voidSomeMethod();intSomeProperty{get;init;}intthis[inti]{get;}eventAction?someEvent;eventActionSomeEvent;}



AutoInterfaceAttribute with explicit interface specifier

usingAutoInterfaceAttributes;[AutoInterface]publicsealedclassExplicitExample:IExplicitExample{voidIExplicitExample.SomeMethod(){}intIExplicitExample.SomeProperty{get;init;}intIExplicitExample.this[inti]=>i;eventActionIExplicitExample.SomeEvent{add{}remove{}}}
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
usingAutoInterfaceAttributes;publicpartialinterfaceIExplicitExample{voidSomeMethod();intSomeProperty{get;init;}intthis[inti]{get;}eventActionSomeEvent;}



multiple AutoInterfaceAttributes on same class

usingAutoInterfaceAttributes;[AutoInterface(Name="IMultipleExample1")][AutoInterface(Name="IMultipleExample2")]publicsealedclassMultipleExample:IMultipleExample1,IMultipleExample2{publicvoidSomeMethod(){}intIMultipleExample1.PropertyFirst{get;set;}stringIMultipleExample2.PropertySecond{get;set;}}
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
usingAutoInterfaceAttributes;publicpartialinterfaceIMultipleExample1{voidSomeMethod();intPropertyFirst{get;set;}}
...// <auto-generated/>
#pragma warning disable
#nullable enable annotations
using AutoInterfaceAttributes;publicpartialinterfaceIMultipleExample2{voidSomeMethod();stringPropertySecond{get;set;}}



AutoInterfaceAttribute with summary documentation

usingAutoInterfaceAttributes;/// <summary>/// my class description/// </summary>[AutoInterface]publicsealedclassSummaryExample{/// <summary>/// some method description/// </summary>publicvoidSomeMethod(){}}
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
usingAutoInterfaceAttributes;/// <summary>/// my class description/// </summary>publicpartialinterfaceISummaryExample{/// <summary>/// some method description/// </summary>voidSomeMethod();}



AutoInterfaceAttribute with generic class

usingAutoInterfaceAttributes;[AutoInterface]publicsealedclassGenericExample<T>{publicTIdentity(Tparameter)=>parameter;}
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
usingAutoInterfaceAttributes;publicpartialinterfaceIGenericExample<T>{TIdentity(Tparameter);}



Parameter

  • Name

Type: string
Default: $"I{ClassName}"

If you want another name for your interface, put it here.

usingAutoInterfaceAttributes;[AutoInterface(Name="NewName")]publicsealedclassExample;
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
usingAutoInterfaceAttributes;publicpartialinterfaceNewName{}



  • Modifier

Type: string
Default: "public partial"

If you want another visible modifier or make the interface non-partial or unsafe, you can do this here.

usingAutoInterfaceAttributes;[AutoInterface(Modifier="internal")]publicsealedclassExample;
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
usingAutoInterfaceAttributes;internalinterfaceIExample{}



  • Namespace

Type: string
Default: $"{ClassNamespace}"

When the generated interface should live in a specific namespace, you can specify it here.
For global namespace, use an empty string.

usingAutoInterfaceAttributes;namespaceMyApp.Core;[AutoInterface(Namespace="MyApp.Utils")]publicsealedclassExample;
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
usingAutoInterfaceAttributes;namespaceMyApp.Utils;publicpartialinterfaceIExample{}



  • Inheritance

Type: Type[]
Default: []

If the generated interface should inherit from one or more other interfaces, you can list them here.

usingAutoInterfaceAttributes;[AutoInterface(Inheritance=[typeof(ICore)])]publicsealedclassExample;publicpartialinterfaceICore{ ...}
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
usingAutoInterfaceAttributes;publicpartialinterfaceIExample:ICore{}



  • Nested

Type: string[]
Default: []

When the interface should be nested inside other classes, structs or interfaces, declare them here.

usingAutoInterfaceAttributes;[AutoInterface(Nested=["public partial class MyWrapper","public partial interface OuterInterface"])]publicsealedclassExample{publicvoidSomeMethod(){}}
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
usingAutoInterfaceAttributes;publicpartialclassMyWrapper{publicpartialinterfaceOuterInterface{publicpartialinterfaceIExample{voidSomeMethod();}}}



  • StaticMembers

Type: bool
Default: false

Normally, static members are just ignored. However, an interface can contain static members as a "static abstract" member.
To accept static members to generate "static abstract" members, set this flag to true.

usingAutoInterfaceAttributes;[AutoInterface(StaticMembers=true)]publicsealedclassExample{publicstaticvoidSomeMethod(){}}
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
usingAutoInterfaceAttributes;publicpartialinterfaceIExample{staticabstractvoidSomeMethod();}



  • Access Modifier

When you want to set the visibility of a specific member, you can decorate it with a [AutoInterfaceVisibility...] attribute.
There are 5 different Visibility attribute:

  • [AutoInterfaceVisibilityPublic]
  • [AutoInterfaceVisibilityInternal]
  • [AutoInterfaceVisibilityProtected]
  • [AutoInterfaceVisibilityProtectedInternal]
  • [AutoInterfaceVisibilityPrivateProtected]
usingAutoInterfaceAttributes;[AutoInterface]publicsealedclassExample:IExample{[AutoInterfaceVisibilityPublic]publicvoidPublicMethod(){}[AutoInterfaceVisibilityInternal]voidIExample.InternalMethod(){}[AutoInterfaceVisibilityProtected]voidIExample.ProtectedMethod(){}[AutoInterfaceVisibilityProtectedInternal]voidIExample.ProtectedInternalMethod(){}[AutoInterfaceVisibilityPrivateProtected]voidIExample.PrivateProtectedMethod(){}}
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
usingAutoInterfaceAttributes;publicpartialinterfaceIExample{[AutoInterfaceVisibilityPublic]publicvoidPublicMethod();[AutoInterfaceVisibilityInternal]internalvoidInternalMethod();[AutoInterfaceVisibilityProtected]protectedvoidProtectedMethod();[AutoInterfaceVisibilityProtectedInternal]protectedinternalvoidProtectedInternalMethod();[AutoInterfaceVisibilityPrivateProtected]privateprotectedvoidPrivateProtectedMethod();}

Note:
The access modifiers private and file are not possible, because private members needs an implementation and file members would not be visible to the outside.



  • IgnoreAutoInterfaceAttribute

When you want a specific member to be ignored by the generator, you can decorate it with [IgnoreAutoInterface].

usingAutoInterfaceAttributes;[AutoInterface]publicsealedclassExample{[IgnoreAutoInterface]publicvoidSomeMethod(){}}
// <auto-generated/>
#pragma warning disable
#nullable enable annotations
usingAutoInterfaceAttributes;publicpartialinterfaceIExample{}



Disable Attribute Generation

You can disable the generation of the attributes by defining a constant for your compilation:

 <PropertyGroup>
<DefineConstants>AUTOINTERFACE_EXCLUDE_ATTRIBUTES</DefineConstants>
</PropertyGroup>

This functionality is specific for the use case when you have a project referencing another project, both projects using this generator and you have InternalsVisibleTo enabled. In that case you have the attributes defined twice in your referencing project and you get a warning about that. By defining this constant in your referencing project, you prevent one generation, so the attributes are only defined once in the referenced project.



Remarks

Using-statements will always be placed on the top, so using not fully-qualified using-statements might cause compile errors.

usingAutoInterfaceAttributes;namespaceSystem.Collections{usingGeneric;// <-- refers to "System.Collections.Generic"[AutoInterface]publicsealedclassExample;}// ...// <auto-generated/>
#pragma warning disable
#nullable enable annotations
usingGeneric;// <-- refers to "Generic"usingAutoInterfaceAttributes;publicpartialinterfaceIExample{}

You also should not use not fully-qualified using-statements in the first place, because they can be ambiguous. By introducing an additional namespace, the referring of a not fully-qualified using-statement might change and your application breaks at unrelated places. Just put your using statements at the top.

About

AutoInterface is a source generator that generates an interface based on your class/struct. Basically, you write your class and get the corresponding interface for free.

Resources

Stars

12 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages