Skip to content

Source generator that generates Default Interface Members on partial types that implement said interface

License

Notifications You must be signed in to change notification settings

CaitaXD/MemberGenerator

Repository files navigation

MemberGenerator

Generates partial interface members inside the type that implements the interface.

Usage

[MemberGenerator.GenerateDefaultMembers]
public interface IDefaultInterface
{
    public void Foo() => Console.WriteLine("Foo");
    public int Bar() => 42;
}

So when implementing the interface

public partial struct MyStruct : IDefaultInterface;

Will generate the following partial type

public partial struct MyStruct
{
    public void Foo() => Console.WriteLine("Foo");
    public int Bar() => 42;
}

Important

The type that implements the interface must be partial, otherwise nothing will be generated.

Use case

Default interface members cannot be called directly from the type that implements the interface, as it requires the instance to be cast to the interface type.

Additionally, if you do this in struct, it can cause it to be boxed.

About

Source generator that generates Default Interface Members on partial types that implement said interface

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages