feat: initial import
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user