refactor
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using ZLinq;
|
||||
|
||||
namespace RebootKit.Engine.Foundation {
|
||||
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Method)]
|
||||
@@ -28,7 +29,9 @@ namespace RebootKit.Engine.Foundation {
|
||||
}
|
||||
|
||||
public void Bind(Type type, object obj) {
|
||||
if (!m_BindingsMaps.TryAdd(type, obj)) s_logger.Error($"Cannot bind to '{type}', slot is already occupied");
|
||||
if (!m_BindingsMaps.TryAdd(type, obj)) {
|
||||
s_logger.Error($"Cannot bind to '{type}', slot is already occupied");
|
||||
}
|
||||
}
|
||||
|
||||
public void Bind<TBind>(TBind obj) {
|
||||
@@ -53,22 +56,27 @@ namespace RebootKit.Engine.Foundation {
|
||||
Inject(instance);
|
||||
return instance;
|
||||
}
|
||||
|
||||
|
||||
public T Create<T>(params object[] args) {
|
||||
T instance = (T) Activator.CreateInstance(typeof(T), args);
|
||||
Inject(instance);
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void Inject<T>(T target) {
|
||||
Type type = typeof(T);
|
||||
|
||||
foreach (FieldInfo field in type.GetFields(k_fieldsBindingFlags)) InjectField(field, target);
|
||||
public void Inject(object target) {
|
||||
Type type = target.GetType();
|
||||
|
||||
foreach (FieldInfo field in type.GetFields(k_fieldsBindingFlags)) {
|
||||
InjectField(field, target);
|
||||
}
|
||||
|
||||
foreach (MethodInfo method in type.GetMethods(k_methodsBindingFlags)) {
|
||||
if (!Attribute.IsDefined(method, typeof(InjectAttribute))) continue;
|
||||
if (!Attribute.IsDefined(method, typeof(InjectAttribute))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Type[] paramsTypes = method.GetParameters()
|
||||
.AsValueEnumerable()
|
||||
.Select(t => t.ParameterType)
|
||||
.ToArray();
|
||||
|
||||
@@ -77,7 +85,9 @@ namespace RebootKit.Engine.Foundation {
|
||||
for (int i = 0; i < paramsTypes.Length; ++i) {
|
||||
instances[i] = Resolve(paramsTypes[i]);
|
||||
|
||||
if (instances[i] == null) s_logger.Error($"Failed to resolve method parameter of type `{paramsTypes[i]}`");
|
||||
if (instances[i] == null) {
|
||||
s_logger.Error($"Failed to resolve method parameter of type `{paramsTypes[i]}`");
|
||||
}
|
||||
}
|
||||
|
||||
method.Invoke(target, instances);
|
||||
@@ -85,9 +95,11 @@ namespace RebootKit.Engine.Foundation {
|
||||
}
|
||||
|
||||
bool InjectField(FieldInfo field, object target) {
|
||||
for (int i = m_FieldInjectors.Count - 1; i >= 0; i--)
|
||||
if (m_FieldInjectors[i].Inject(field, target, this))
|
||||
for (int i = m_FieldInjectors.Count - 1; i >= 0; i--) {
|
||||
if (m_FieldInjectors[i].Inject(field, target, this)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user