Example #1
0
        private ParameterInfoCache[] GetParameterInfoCache(ParameterInfo[] parameters)
        {
            if (parameters == null || parameters.Length == 0)
            {
                return(null);
            }

            ParameterInfoCache[] parametersCache = new ParameterInfoCache[parameters.Length];

            for (int i = 0; i < parameters.Length; i++)
            {
                ParameterInfo parameterInfo = parameters[i];

                object[] injectAttributes = parameterInfo.GetCustomAttributes(typeof(InjectAttribute), true);

                InjectAttribute injectAttribute = null;

                if (injectAttributes.Length > 0)
                {
                    injectAttribute = (InjectAttribute)injectAttributes[0];
                }

                parametersCache[i] = new ParameterInfoCache(parameters[i], injectAttribute);
            }

            return(parametersCache);
        }
Example #2
0
        private void InjectMethod(IContainer container, IInjectContext parentContext, Type type, object obj, MethodInfoCache method)
        {
            ParameterInfoCache[] parameters = method.Parameters;
            if (parameters == null || parameters.Length == 0)
            {
                method.MethodInfo.Invoke(obj, null);
                return;
            }

            object[] paramObjects = new object[parameters.Length];

            for (int i = 0; i < parameters.Length; i++)
            {
                ParameterInfoCache parameter     = parameters[i];
                ParameterInfo      parameterInfo = parameter.ParameterInfo;

                // Can we resolve this?
                if (container.HasBindingFor(parameterInfo.ParameterType))
                {
                    // Prevent recursive / circular dependency...
                    if (parameterInfo.ParameterType.IsAssignableFrom(type) || _currentlyResolvingTypes.Contains(parameterInfo.ParameterType))
                    {
                        LogHandler?.Invoke("Injector: Circular dependency detected in Method {0}.{1}, parameter index: {2}", type, method.MethodInfo.Name, i);
                        paramObjects[i] = null;
                    }
                    else
                    {
                        InjectContext injectContext = new InjectContext(container, method.DeclaringType, parentContext);

                        Type parameterType = parameterInfo.ParameterType;

                        object paramInstance = container.ResolveWithCategory(parameterType, parameter.InjectAttribute?.Category, injectContext);
                        paramObjects[i] = paramInstance;
                    }
                }
                else
                {
                    LogHandler?.Invoke("Injector: {0} - Type is not bound in the container, assigning as null {1}, method {2}, parameter index: {2}", type, parameterInfo.ParameterType, method.MethodInfo.Name, i);
                    paramObjects[i] = null;
                }
            }

            method.MethodInfo.Invoke(obj, paramObjects.ToArray());
        }
Example #3
0
        private object ExecuteConstructor(IContainer container, IInjectContext parentContext, ConstructorInfoCache constructor, Type type, params object[] addDependencies)
        {
            ParameterInfoCache[] parameters = constructor.Parameters;

            if (parameters == null || parameters.Length == 0)
            {
                return(constructor.ConstructorInfo.Invoke(null));
            }

            // Resolve parameters

            object[] paramObjects = new object[parameters.Length];

            for (int i = 0; i < parameters.Length; i++)
            {
                ParameterInfoCache parameter     = parameters[i];
                ParameterInfo      parameterInfo = parameter.ParameterInfo;

                // Can we resolve this?
                if (container.HasBindingFor(parameterInfo.ParameterType))
                {
                    bool isCircularDependency = false;

                    foreach (Type resolvingType in _currentlyResolvingTypes)
                    {
                        if (parameterInfo.ParameterType.IsAssignableFrom(resolvingType))
                        {
                            isCircularDependency = true;
                        }
                    }

                    // Prevent recursive / circular dependency...
                    if (isCircularDependency)
                    {
                        LogHandler?.Invoke("Injector: Circular dependency detected in Type {0} parameter index: {1}", type, i);
                        paramObjects[i] = null;
                    }
                    else
                    {
                        InjectContext injectContext = new InjectContext(container, constructor.DeclaringType, parentContext);

                        object paramInstance = container.ResolveWithCategory(parameterInfo.ParameterType, parameters[i].InjectAttribute?.Category, injectContext);
                        paramObjects[i] = paramInstance;
                    }
                }
                else
                {
                    bool hasAdditionalDependency = false;

                    // Check additional dependencies...
                    foreach (object dependency in addDependencies)
                    {
                        if (parameterInfo.ParameterType.IsInstanceOfType(dependency))
                        {
                            hasAdditionalDependency = true;
                            paramObjects[i]         = dependency;

                            break;
                        }
                    }

                    if (!hasAdditionalDependency)
                    {
                        LogHandler?.Invoke("Injector: {0} - Type is not bound in the container, assigning as null {1}, parameter index: {2}", type, parameterInfo.ParameterType, i);
                        paramObjects[i] = null;
                    }
                }
            }


            // Invoke constructor...
            object instance = constructor.ConstructorInfo.Invoke(paramObjects);

            return(instance);
        }