using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace IFramework.Inject
{
///
/// 注入模块
///
public partial class InjectModule : Module, IInjectModule
{
private InjectTypeMap _type;
private InjectInstanceMap _instance;
#pragma warning disable CS1591 // 缺少对公共可见类型或成员的 XML 注释
protected override ModulePriority OnGetDefautPriority()
{
return ModulePriority.Config;
}
protected override void Awake()
{
_type = new InjectTypeMap();
_instance = new InjectInstanceMap();
}
protected override void OnDispose()
{
this.Clear();
}
#pragma warning restore CS1591 // 缺少对公共可见类型或成员的 XML 注释
///
/// 清除
///
public void Clear()
{
this._instance.Clear();
this._type.Clear();
}
///
/// 反射注入
///
///
public void Inject(object obj)
{
if (obj == null)
{
return;
}
MemberInfo[] members = obj.GetType().GetMembers(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
for (int i = members.Length - 1; i >= 0; i--)
{
MemberInfo member = members[i];
if (member.IsDefined(typeof(InjectAttribute), true))
{
InjectAttribute attr = member.GetCustomAttributes(typeof(InjectAttribute), true)[0] as InjectAttribute;
if (member is PropertyInfo)
{
PropertyInfo propertyInfo = member as PropertyInfo;
propertyInfo.SetValue(obj, this.GetValue(propertyInfo.PropertyType, attr.name, new object[0]), null);
}
else if (member is FieldInfo)
{
FieldInfo fieldInfo = member as FieldInfo;
fieldInfo.SetValue(obj, this.GetValue(fieldInfo.FieldType, attr.name, new object[0]));
}
}
}
}
///
/// 注入所有
///
public void InjectInstances()
{
foreach (object instance in this._instance.Values)
{
this.Inject(instance);
}
}
///
/// 注册
///
///
///
public void Subscribe(string name = null)
{
this.Subscribe(typeof(Type), typeof(Type), name);
}
///
/// 注册
///
///
///
///
public void Subscribe(string name = null) where Type : BaseType
{
this.Subscribe(typeof(BaseType), typeof(Type), name);
}
///
/// 注册
///
///
///
///
public void Subscribe(Type source, Type target, string name = null)
{
this._type.Set(source, name, target);
}
///
/// 注册实例
///
///
///
///
///
public void SubscribeInstance(Type instance, string name, bool inject = true) where Type : class
{
this.SubscribeInstance(typeof(Type), instance, name, inject);
}
///
/// 注册实例
///
///
///
///
///
///
public void SubscribeInstance(Type baseType, object instance, string name, bool inject = true)
{
Type type = instance.GetType();
if (type != baseType && !type.IsExtendInterface(baseType) && !type.IsSubclassOf(baseType))
{
throw new Exception(string.Format("{0} is Not {1}", type, baseType));
}
this._instance.Set(baseType, name, instance);
if (inject)
{
this.Inject(instance);
}
}
///
/// 获取
///
///
///
///
///
public T GetValue(string name = null, params object[] args) where T : class
{
return (T)GetValue(typeof(T), name, args);
}
///
/// 获取
///
///
///
///
///
public object GetValue(Type baseType, string name = null, params object[] constructorArgs)
{
object item = _instance.Get(baseType, name);
if (item != null) return item;
Type map = this._type.Get(baseType, name);
if (map != null)
{
return this.CreateInstance(map, constructorArgs);
}
return null;
}
///
/// 获取
///
///
///
public IEnumerable