29 lines
969 B
C#
29 lines
969 B
C#
using System;
|
|
using System.Reflection;
|
|
using Godot;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace GodotHostTest.GodotDI;
|
|
|
|
public partial class Scope : Node
|
|
{
|
|
[Export] protected Node?[] InjectedNodes = [];
|
|
|
|
internal void ResolveInjectedNodes(IServiceProvider serviceProvider)
|
|
{
|
|
// resolve the injected nodes
|
|
// TODO: move to source generator, make it the only way for injection
|
|
foreach (var node in InjectedNodes)
|
|
{
|
|
if (node == null) continue;
|
|
foreach (var field in node.GetType()
|
|
.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
|
|
{
|
|
if (field.GetCustomAttribute<InjectAttribute>() == null) continue;
|
|
var fieldType = field.FieldType;
|
|
var service = serviceProvider.GetRequiredService(fieldType);
|
|
field.SetValue(node, service);
|
|
}
|
|
}
|
|
}
|
|
} |