Пример #1
0
        private void mapSetters(ReflectedClass reflected, Type type)
        {
#if UNITY_EDITOR
            //only for [Inject] private properties todo set prebuild step

            /*var privateMembers = type.FindMembers(
             *  MemberTypes.Property,
             *  BindingFlags.FlattenHierarchy |
             *  BindingFlags.SetProperty |
             *  BindingFlags.NonPublic |
             *  BindingFlags.Instance,
             *  null, null);
             *
             * foreach (var member in privateMembers)
             * {
             *  var injections = member.GetCustomAttributes(typeof(Inject), true);
             *  if (injections.Length > 0)
             *  {
             *      throw new ReflectionException(
             *          "The class " + type.Name + " has a non-public Injection setter " + member.Name + ". Make the setter public to allow injection.",
             *          ReflectionExceptionType.CANNOT_INJECT_INTO_NONPUBLIC_SETTER);
             *  }
             * }*/
#endif
            var members = type.FindMembers(
                MemberTypes.Property,
                BindingFlags.FlattenHierarchy |
                BindingFlags.SetProperty |
                BindingFlags.Public |
                BindingFlags.Instance,
                null, null);

            var pairs = new KeyValuePair <Type, PropertyInfo> [0];
            var names = new object[0];

            for (var index = 0; index < members.Length; index++)
            {
                var member = members[index];
                if (member.IsDefined(typeof(Inject), true))
                {
                    var injections = member.GetCustomAttributes(typeof(Inject), true);
                    var attr       = injections[0] as Inject;
                    var point      = member as PropertyInfo;
                    var pointType  = point.PropertyType;
                    var pair       = new KeyValuePair <Type, PropertyInfo>(pointType, point);
                    pairs = AddKV(pair, pairs);

                    var bindingName = attr.name;
                    names = Add(bindingName, names);
                }
            }

            reflected.Setters     = pairs;
            reflected.SetterNames = names;
        }
Пример #2
0
        public ReflectedClass Get(Type type)
        {
            var binding = GetBinding(type);

            if (binding == null)
            {
                binding = GetRawBinding();

                var reflected = new ReflectedClass();
                reflected.Type = type;
                mapPreferredConstructor(reflected, type);
                mapSetters(reflected, type); //map setters before mapping methods
                mapMethods(reflected, type);

                binding.Bind(type).To(reflected);
            }

            return(binding.value as ReflectedClass);
        }
        public IReflectedClass Get(Type type)
        {
            IBinding        binding = GetBinding(type);
            IReflectedClass retv;

            if (binding == null)
            {
                binding = GetRawBinding();
                IReflectedClass reflected = new ReflectedClass();
                mapPreferredConstructor(reflected, binding, type);
                mapPostConstructors(reflected, binding, type);
                mapSetters(reflected, binding, type);
                binding.Bind(type).To(reflected);
                retv = binding.value as IReflectedClass;
                retv.PreGenerated = false;
            }
            else
            {
                retv = binding.value as IReflectedClass;
                retv.PreGenerated = true;
            }
            return(retv);
        }
Пример #4
0
        private void mapMethods(ReflectedClass reflected, Type type)
        {
            var methods = type.GetMethods(
                BindingFlags.FlattenHierarchy |
                BindingFlags.Public |
                BindingFlags.Instance |
                BindingFlags.InvokeMethod);

            for (var index = 0; index < methods.Length; index++)
            {
                var method       = methods[index];
                var hasAttribute = method.IsDefined(typeof(PostConstruct));
                if (hasAttribute)
                {
                    if (reflected.PostConstructor != null)
                    {
                        throw new InjectionException("Class has already Posconstruct " + type,
                                                     InjectionExceptionType.ILLIGAL_USAGE);
                    }

                    reflected.PostConstructor = method;
                    continue;
                }

                hasAttribute = method.IsDefined(typeof(Deconstruct));
                if (hasAttribute)
                {
                    if (reflected.DeConstructor != null)
                    {
                        throw new InjectionException("Class has already Deconstruct " + type,
                                                     InjectionExceptionType.ILLIGAL_USAGE);
                    }

                    reflected.DeConstructor = method;
                }
            }
        }
Пример #5
0
        private void mapPreferredConstructor(ReflectedClass reflected, Type type)
        {
            var constructor = findPreferredConstructor(type);

            if (constructor == null)
            {
                throw new ReflectionException(
                          "The reflector requires concrete classes.\nType " + type +
                          " has no constructor. Is it an interface?",
                          ReflectionExceptionType.CANNOT_REFLECT_INTERFACE);
            }

            var parameters = constructor.GetParameters();

            var paramList = new Type[parameters.Length];
            var names     = new object[parameters.Length];
            var i         = 0;

            foreach (var param in parameters)
            {
                var paramType = param.ParameterType;
                paramList[i] = paramType;

                if (param.IsDefined(typeof(Name), true))
                {
                    var attributes = param.GetCustomAttributes(typeof(Name), false);
                    names[i] = ((Name)attributes[0]).name;
                }

                i++;
            }

            reflected.Constructor               = constructor;
            reflected.ConstructorParameters     = paramList;
            reflected.ConstructorParameterNames = names;
        }