Beispiel #1
0
        private void TryToAutoRegisterBinding(string name, Type type)
        {
            ConstructorInfo defaultConstructor    = null;
            ConstructorInfo injectableConstructor = null;

            foreach (ConstructorInfo constructorInfo in type.GetConstructors())
            {
                ParameterInfo[] parameterInfos = constructorInfo.GetParameters();
                if (parameterInfos.Length == 0)
                {
                    defaultConstructor = constructorInfo;
                }
                if (TypeHelper.HasAttribute <InjectAttribute>(constructorInfo))
                {
                    if (injectableConstructor != null)
                    {
                        throw new Exception("Multiple injectable constructors found in type " + type + ". Only one is allowed.");
                    }
                    injectableConstructor = constructorInfo;
                }
            }
            ConstructorInfo constructor = null;

            if (injectableConstructor != null)
            {
                constructor = injectableConstructor;
            }
            else if (defaultConstructor != null)
            {
                constructor = defaultConstructor;
            }
            else
            {
                throw new Exception("Unable to auto bind type " + type + ". No injectable constructor and no default constructor found.");
            }
            IBinding binding;

            if (type.IsSubclassOf(typeof(Component)))
            {
                binding = new AutoRegisteredComponentBinding(this, name, type);
            }
            else
            {
                binding = new AutoRegisteredPocoBinding(this, name, type, constructor);
            }
            if (TypeHelper.HasAttribute <SingletonAttribute>(type))
            {
                binding = new SingletonBindingDecorator(binding);
            }
            AddBinding(binding);
        }
Beispiel #2
0
        public void RegisterModule(object module)
        {
            foreach (MethodInfo methodInfo in TypeHelper.AllMethodsOf(module.GetType()))
            {
                ProvidesAttribute providesAttribute;
                if (TypeHelper.TryToGetAttribute(methodInfo, out providesAttribute))
                {
                    string name = providesAttribute.name;
                    Type   type = (providesAttribute.interfaceType != null ? providesAttribute.interfaceType : methodInfo.ReturnType);

                    IBinding binding = new ModuleBinding(this, name, type, methodInfo, module);
                    if (TypeHelper.HasAttribute <SingletonAttribute>(methodInfo))
                    {
                        binding = new SingletonBindingDecorator(binding);
                    }
                    AddBinding(binding);
                }
            }
        }