Пример #1
0
        /// <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());
        }
Пример #2
0
        /// <summary>
        /// 返回接受注入的属性信息 : type 为 key,object 为 id, PropertyInfo 为值
        /// </summary>
        virtual protected SetterInfo[] GetProperties(Type type)
        {
            var setters = new List <SetterInfo>();

            // 获取参数 type 的属性
            var properties = type.GetProperties(
                BindingFlags.Instance |
                BindingFlags.Static |
                BindingFlags.NonPublic |
                BindingFlags.Public);

            for (int i = 0; i < properties.Length; i++)
            {
                var property   = properties[i] as PropertyInfo;
                var attributes = property.GetCustomAttributes(typeof(Inject), true);

                if (attributes.Length > 0)
                {
                    var attribute = attributes[0] as Inject;
                    var method    = MethodUtils.CreatePropertySetter(type, property);
                    var info      = new SetterInfo(property.PropertyType, attribute.id, method);
                    setters.Add(info);
                }
            }

            return(setters.ToArray());
        }