Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/LokiMidgard/AutoInvoke.Generator
A generator that generates a method that invokes, a specified generic method, for ever Type in your project that sattisfis a defined constraint
https://github.com/LokiMidgard/AutoInvoke.Generator
csharp-sourcegenerator sourcegenerator
Last synced: 3 months ago
JSON representation
A generator that generates a method that invokes, a specified generic method, for ever Type in your project that sattisfis a defined constraint
- Host: GitHub
- URL: https://github.com/LokiMidgard/AutoInvoke.Generator
- Owner: LokiMidgard
- License: unlicense
- Created: 2023-08-12T19:33:55.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2024-04-25T18:26:07.000Z (6 months ago)
- Last Synced: 2024-05-12T13:31:47.606Z (6 months ago)
- Topics: csharp-sourcegenerator, sourcegenerator
- Language: C#
- Homepage:
- Size: 84 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE.txt
Awesome Lists containing this project
- RSCG_Examples - https://github.com/LokiMidgard/AutoInvoke.Generator
- csharp-source-generators - AutoInvoke - ![stars](https://img.shields.io/github/stars/LokiMidgard/AutoInvoke.Generator?style=flat-square&cacheSeconds=604800) ![last commit](https://img.shields.io/github/last-commit/LokiMidgard/AutoInvoke.Generator?style=flat-square&cacheSeconds=86400) A generator that generates a method that invokes a specified generic method, for every Type in your project that satisfies a defined constraint. (Source Generators / Other)
README
[![NuGet](https://img.shields.io/nuget/v/AutoInvoke.Generator.svg?style=flat-square)](https://www.nuget.org/packages/AutoInvoke.Generator/)
[![GitHub license](https://img.shields.io/github/license/LokiMidgard/AutoInvoke.Generator.svg?style=flat-square)](https://tldrlegal.com/license/mit-license#summary)
# AutoInvokeThis 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:
```c#
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:
```c#
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:
```c#
internal delegate IFileLoder LoadFile(string path);
internal partial class FileHandler {
private readonly Dictionary 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() 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:
```c#
partial class FileHandler {
private void LoadLoaders() {
LoadLoaders();
}
}
```## Featurs and limitations
- You can control wich type of types shold get called. E.g. by
default no calls are generated for abstract classes or types defined in referenced Assemblys. But you can overide this setting
- The anotated method can be static
- If the anotated method has parameters the generated method has the same parametrs
- If the return type is not `void` the generated methods returntype is an array of the return type of the attributed method### Limitations
- When using multiple Type Parameters, one Type Parameter must contain all others (transitiv) like `Foo() where T1: IComparable where T2 : IComparable`
- You can't call static Types. Generics do not allow this.