Basilisque.AutoImplementer.CodeAnalysis 1.0.0-RC9

This is a prerelease version of Basilisque.AutoImplementer.CodeAnalysis.
dotnet add package Basilisque.AutoImplementer.CodeAnalysis --version 1.0.0-RC9                
NuGet\Install-Package Basilisque.AutoImplementer.CodeAnalysis -Version 1.0.0-RC9                
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="Basilisque.AutoImplementer.CodeAnalysis" Version="1.0.0-RC9" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Basilisque.AutoImplementer.CodeAnalysis --version 1.0.0-RC9                
#r "nuget: Basilisque.AutoImplementer.CodeAnalysis, 1.0.0-RC9"                
#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.
// Install Basilisque.AutoImplementer.CodeAnalysis as a Cake Addin
#addin nuget:?package=Basilisque.AutoImplementer.CodeAnalysis&version=1.0.0-RC9&prerelease

// Install Basilisque.AutoImplementer.CodeAnalysis as a Cake Tool
#tool nuget:?package=Basilisque.AutoImplementer.CodeAnalysis&version=1.0.0-RC9&prerelease                

Basilisque - Auto Implementer

Overview

This project provides functionality to automatically implement interfaces.

NuGet Basilisque.AutoImplementer.CodeAnalysis License

Description

This project contains a source generator that automatically implements interface members in classes implementing the interfaces.
The goal is to provide a workaround for C# not supporting multiple inheritance for some basic use cases.

Getting Started

Install the NuGet package Basilisque.AutoImplementer.
Installing the package will add the source generator to your project.

Now you're ready to start implementing your interfaces automatically.

Features

General

  • Automatic implementation of interfaces on classes

  • Classes have to be marked with the [AutoImplementInterfaces] attribute

    • Then all interfaces marked with the [AutoImplementInterface] attribute will be implemented
      [AutoImplementInterface]
      public interface ITitle
      {
        string Title { get; set; }
      }
      
      public interface ISummary
      {
        string Summary { get; set; }
      }
      
      [AutoImplementInterfaces]
      public partial class Book : ITitle, ISummary
      { /* will have the property 'Title' only. 'ISummary' is not marked with an attribute */ }
      
    • By specifying the interfaces explicitly within the attribute on the class, the interfaces dont't have to be marked
      ([AutoImplementInterfaces<IInterface>()] or [AutoImplementInterfaces(typeof(IInterface))])
      public interface ITitle
      {
        string Title { get; set; }
      }
      
      public interface ISummary
      {
        string Summary { get; set; }
      }
      
      [AutoImplementInterfaces<ITitle, ISummary>()]
      //[AutoImplementInterfaces(typeof(ITitle), typeof(ISummary))] <- alternative syntax
      public partial class Book : ITitle, ISummary
      { /* will have the properties 'Title' and 'Summary' */ }
      
  • When the interfaces are explicitly stated, they don't have to be stated as base type a second time. This is valid:

    public interface ITitle
    {
      string Title { get; set; }
    }
    
    [AutoImplementInterfaces<ITitle>()]
    //[AutoImplementInterfaces(typeof(ITitle))] <- alternative syntax
    public partial class Book // ': ITitle' <- this is optional because 'ITitle' was specified in the attribute
    { }
    

Property Implementation

  • Properties of interfaces will be added as auto-implemented properties
  • Either single properties or the whole interface can be marked to implement the properties as required.
    public interface IBook
    {
      [Required] string Title { get; set; } // implements 'Title' as 'required'
    
      DateOnly PublishDate { get; set; }    // not required
    
      [AutoImplement(AsRequired = true)]    // implements 'Publisher' as 'required'
      string Publisher { get; set; }
    }
    
    [AutoImplementInterface(ImplementAllPropertiesAsRequired = true)] // implements all properties of 'IMovie' as 'required'
    public interface IMovie
    {
      string Title { get; set; }
    
      string Summary { get; set; }
    }
    
  • Properties can be skipped. Then they have to be implemented manually.
    public interface IPublish
    {
      DateOnly PublishDate { get; set; }
    
      [AutoImplement(Implement = false)] // skips the 'Publisher' property
      string? Publisher { get; set; }
    }
    

Examples

Create the interfaces:

[AutoImplementInterface]
public interface ITitle
{
  [Required] string Title { get; set; }
}

public interface IDetails
{
  byte[]? Image { get; set; }  
  string Summary { get; set; }  
}

Create some classes implementing the interfaces:

[AutoImplementInterfaces()] // <- no interfaces were stated explicitly. 'ITitle' will be implemented because it is marked with the 'AutoImplementInterface' attribute
public partial class Book : ITitle, IDetails
{ /* will have the properties Title only */ }

[AutoImplementInterfaces<IDetails>()] // <- 'IDetails' was stated explicitly; only this interface will be implemented
public partial class Movie : ITitle, IDetails
{ /* will have the properties Image and Summary */ }

[AutoImplementInterfaces<IDetails>()] // <- will implement 'IDetails'
[AutoImplementInterfaces()]           // <- will find 'ITitle' because it is marked with the 'AutoImplementInterface' attribute
public partial class Game : ITitle, IDetails
{ /* will have the properties Title, Image and Summary */ }

[AutoImplementInterfaces(typeof(ITitle), typeof(IDetails))]
public partial class Event
{ /* will have the properties Title, Image and Summary */ }

The source generator now adds the members to the corresponding classes without you having to do this on your own every time.

For details see the wiki.

License

The Basilisque framework (including this repository) is licensed under the Apache License, Version 2.0.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • 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.0-RC9 43 10/25/2024