/// <summary> /// Resolves the fields that can be injected. /// </summary> /// <param name="type">Type from which reflection will be resolved.</param> /// <returns>The fields.</returns> protected AcessorInfo[] ResolveFields(Type type) { var setters = new List <AcessorInfo>(); var fields = type.GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); for (int fieldIndex = 0; fieldIndex < fields.Length; fieldIndex++) { var field = fields[fieldIndex] as FieldInfo; var attributes = field.GetCustomAttributes(typeof(Inject), true); if (attributes.Length > 0) { var attribute = attributes[0] as Inject; var getter = MethodUtils.CreateFieldGetter(type, field); var setter = MethodUtils.CreateFieldSetter(type, field); var info = new AcessorInfo(field.FieldType, field.Name, attribute.identifier, getter, setter); setters.Add(info); } } return(setters.ToArray()); }
/// <summary> /// 返回接受注入的字段信息 : type 为 key,object 为 id, PropertyInfo 为值 /// </summary> virtual protected SetterInfo[] GetFields(Type type) { var setters = new List <SetterInfo>(); // 获取参数 type 的属性 var fields = type.GetFields( BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public); for (int i = 0; i < fields.Length; i++) { var field = fields[i] as FieldInfo; var attributes = field.GetCustomAttributes(typeof(Inject), true); if (attributes.Length > 0) { var attribute = attributes[0] as Inject; var method = MethodUtils.CreateFieldSetter(type, field); var info = new SetterInfo(field.FieldType, attribute.id, method); setters.Add(info); } } return(setters.ToArray()); }