RSCG – AutoInvoke.Generator
| name | AutoInvoke.Generator |
| nuget | https://www.nuget.org/packages/AutoInvoke.Generator/ |
| link | https://github.com/LokiMidgard/AutoInvoke.Generator |
| author | Patrick Kranz |
Finding all implementation of an interface/class and invoke them.
This is how you can use AutoInvoke.Generator .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoInvoke.Generator" Version="0.0.9">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
The code that you will use is
using AutoAdd;
RemoteCollection rc=new();
foreach(var item in rc.loaders)
{
item.Execute();
}
namespace AutoAdd;
partial class RemoteCollection
{
public List<IRemoteCommand> loaders = new ();
public RemoteCollection()
{
LoadLoaders();
}
[AutoInvoke.FindAndInvoke]
public void LoadLoaders<T>() where T : IRemoteCommand,new()
{
loaders.Add(new T());
}
}
namespace AutoAdd;
internal class PCRemote : IRemoteCommand
{
public void Execute()
{
Console.WriteLine("start PC");
}
}
namespace AutoAdd;
internal class TVRemote : IRemoteCommand
{
public void Execute()
{
Console.WriteLine("start TV");
}
}
The code that is generated is
// <auto-generated/>
#nullable enable
namespace AutoInvoke;
[System.AttributeUsage(System.AttributeTargets.Method,Inherited = false,AllowMultiple = true)]
[System.Diagnostics.Conditional("AutoNotifyGenerator_DEBUG")]
internal sealed class FindAndInvokeAttribute : System.Attribute
{
#pragma warning disable CS0169 // Remove unused parameter
#pragma warning disable IDE0060 // Remove unused parameter
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public FindAndInvokeAttribute()
{
}
public FindAndInvokeAttribute(string pattern)
{
}
public bool ScanExternalAssamblies { get; set; }
public string MethodName { get; set; }
public bool CallForAbstractClasses { get; set; }
public bool CallForInterfaces { get; set; }
public bool CallForStructs { get; set; }
public bool CallForClasses { get; set; }
public bool CallForRecords { get; set; }
#pragma warning restore CS0169 // Remove unused parameter
#pragma warning restore IDE0060 // Remove unused parameter
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
}
// <auto-generated/>
#nullable enable
namespace AutoAdd;
partial class RemoteCollection {
private void LoadLoaders() {
LoadLoaders<global::AutoAdd.PCRemote>();
LoadLoaders<global::AutoAdd.TVRemote>();
}
}
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/AutoInvoke.Generator
Leave a Reply