summaryrefslogtreecommitdiff
path: root/compiler/ILEmitter/DIConfiguration.cs
blob: e92b1da8d8c21ed1e835f50ba179247f772972ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
using System.Reflection.Emit;

namespace AtLangCompiler.ILEmitter;

internal static class DIConfiguration
{
    public static IServiceProvider ConfigureServices(ILGenerator il, LocalBuilder dictLocal, ILMethodEmitterManager emitter)
    {
        ServiceCollection serviceCollection = new ServiceCollection();

        // Register shared dependencies
        serviceCollection.AddSingleton(il);
        serviceCollection.AddSingleton(dictLocal);
        serviceCollection.AddSingleton(emitter);

        // Register all emitters dynamically
        IEnumerable<Type> emitterTypes = Assembly.GetExecutingAssembly()
            .GetTypes()
            .Where(t => t.CustomAttributes.Any(e => e.AttributeType == typeof(EmitterForAttribute)));

        foreach (Type? type in emitterTypes)
        {
            serviceCollection.AddTransient(type);
        }

        return serviceCollection.BuildServiceProvider();
    }
}