AutoInvoke.Generator 0.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package AutoInvoke.Generator --version 0.0.2
NuGet\Install-Package AutoInvoke.Generator -Version 0.0.2
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="AutoInvoke.Generator" Version="0.0.2">
  <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.
paket add AutoInvoke.Generator --version 0.0.2
#r "nuget: AutoInvoke.Generator, 0.0.2"
#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 AutoInvoke.Generator as a Cake Addin
#addin nuget:?package=AutoInvoke.Generator&version=0.0.2

// Install AutoInvoke.Generator as a Cake Tool
#tool nuget:?package=AutoInvoke.Generator&version=0.0.2

NuGet GitHub license

AutoInvoke

This Generator let you anotate an Parameterless Generic Method with exactly one TypeArgument.

It will then generate a method with the same name and no type arguments that calls your anotated method for every (non static) Type decleared in your project, that satisfies the type constraints.

Sample

Assume you have the following Interface:

internal interface IFileLoder {
    public abstract static IFileLoder Init(string path);
    public abstract static string FileExtension { get; }
}

This describes a File loader for different types in our project.

And following implementation:

internal class AudioLoader : IFileLoder {
    public static string FileExtension => ".mp3";

    public static IFileLoder Init(string Path) {
        return new AudioLoader(path);
    }
    // the rest of the code...
}

Which defines how we want to load mp3 files.

We now want to automaticly get a list of all IFileLoader so we know what files we can handle, and we do not want to manualy handel such a list.

An Implementation could look like this:

internal delegate IFileLoder LoadFile(string path);
internal partial class FileHandler {
    private readonly Dictionary<string, LoadFile> loaders = new();

    public FileHandler() {
        LoadLoaders();
    }

    public void LoadFile(string file) {
        if (loaders.TryGetValue(Path.GetExtension(file), out var loaderFactory)) {
            var loader = loaderFactory(file);
            // use loader to do things
        }
    }


    [AutoInvoke.Generator.FindAndInvoke]
    public void LoadLoaders<T>() where T : IFileLoder {
        this.loaders.Add(T.FileExtension, T.Init);
    }
}

The field loaders will have all extensions our code can handle, and has to every extension the corresponding Init-Method.

The Generated code will look like this:

partial class FileHandler {
    private void LoadLoaders() {
        LoadLoaders<AutoInvoke.Generator.Example.AudioLoader>();
    }
}

Featurs and limitations

  • You can control wich type of types shold get called. E.g. by defaurd no calls are generated for abstract classes. But you can overide this setting
  • The anotated method can be static
  • The return type can be void or Task. Tasks will be awaited.

Limitations

  • Only one Typeparameter is supported you can't do somthing like Foo<T1,T2>() where T1: IComparable<T2>
  • You can't call static classes. Generics do not allow this.

Possible Featurs

  • Allow other return types and return an array of the results.
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
0.0.11 104 4/25/2024
0.0.10 91 4/25/2024
0.0.9 255 8/27/2023
0.0.8 217 8/25/2023
0.0.7 199 8/23/2023
0.0.6 138 8/22/2023
0.0.5 227 8/22/2023
0.0.4 190 8/20/2023
0.0.3 268 8/14/2023
0.0.2 258 8/13/2023
0.0.1 210 8/12/2023