Example #1
0
        private InjectionConstructor ResolvableConstructor(ConstructorInfo[] constructorInfos)
        {
            InjectionConstructor[] injectionConstructors = new InjectionConstructor[constructorInfos.Length];
            int validIndex = -1, max = -1;

            for (int i = 0; i < constructorInfos.Length; i++)
            {
                ConstructorInfo      constructorInfo      = constructorInfos[i];
                InjectionConstructor injectionConstructor = MappingConstructor(constructorInfo);
                if (injectionConstructor.Resolvable(this))
                {
                    injectionConstructors[i] = injectionConstructor;
                    int parameterCount = constructorInfo.GetParameters()?.Length ?? 0;
                    if (parameterCount > max)
                    {
                        max        = parameterCount;
                        validIndex = i;
                    }
                }
            }
            return(validIndex > -1 ? injectionConstructors[validIndex] : null);
        }
Example #2
0
        private object CreateInstance(Type to)
        {
            var constructorInfos = to.GetConstructors();

            if (constructorInfos?.Length > 0)
            {
                InjectionConstructor resolvableConstructor = ResolvableConstructor(constructorInfos);
                if (resolvableConstructor != null)
                {
                    if (resolvableConstructor.ParameterTypes != null)
                    {
                        object[] parameters = Resolve(resolvableConstructor.ParameterTypes);
                        return(Activator.CreateInstance(to, parameters));
                    }
                    else
                    {
                        return(Activator.CreateInstance(to));
                    }
                }
            }
            return(null);
        }