-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoInjectorList.cs
42 lines (35 loc) · 1.03 KB
/
AutoInjectorList.cs
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
31
32
33
34
35
36
37
38
39
40
41
42
using System.Reflection;
using System.Linq;
using TypeInspector;
using UnityEngine;
using Zenject;
namespace AutoInjector
{
public class AutoInjectorList : MonoBehaviour
{
public TypeReference Type;
[PropertyFilter(nameof(FilterTargetProperty))]
public MonoPropertyReference[] TargetProperties;
[Inject]
public void Inject(DiContainer ctx)
{
if (!Type.IsValid())
{
Debug.LogError("Type is invalid!", this);
}
if (!TargetProperties.All(p => p.IsValid()))
{
Debug.LogError("Target property is invalid!", this);
}
var instance = ctx.Resolve(Type.Get());
foreach(var target in TargetProperties)
{
target.Set(instance);
}
}
private bool FilterTargetProperty(PropertyInfo property)
{
return Type.IsValid() && property.PropertyType.IsAssignableFrom(Type.Get());
}
}
}