Esempio n. 1
0
        public ICollection <InstanceWrapper> GetInstances(CycleCounter cycleCounter, object[] args)
        {
            var resultArgs = prePassedArgs.Length > args.Length ? prePassedArgs : args;

            switch (scope)
            {
            case Scope.Transient:
                var newInstances = CreateInstances(cycleCounter, resultTypes, resultArgs);

                foreach (var newInstance in newInstances)
                {
                    instances.Add(newInstance);
                }

                return(newInstances);

            case Scope.Singleton:
                for (var i = 0; i < resultTypes.Length; i++)
                {
                    var resultType = resultTypes[i];

                    if (i >= instances.Count)
                    {
                        instances.Add(CreateInstance(cycleCounter, resultType, args));
                    }
                }

                return(instances);

            default:
                throw new ArgumentException(nameof(scope));
            }
        }
Esempio n. 2
0
        private void ResolveMethods(CycleCounter cycleCounter, object instance)
        {
            var type              = instance.GetType();
            var methods           = type.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
            var injectableMethods = methods.Where(method => method.GetCustomAttributes(container.injectAttributeType).Any()).ToArray();

            foreach (var method in injectableMethods)
            {
                if (CheckNeedsInjectIntoMember(method, instance))
                {
                    var args       = new List <object>();
                    var parameters = method.GetParameters();

                    foreach (var parameter in parameters)
                    {
                        var values    = container.ResolveMultiple(cycleCounter, parameter.ParameterType);
                        var collected = CollectValue(parameter.ParameterType, values);
                        var value     = collected.Value;

                        args.Add(value);
                    }

                    method.Invoke(instance, args.ToArray());

                    MarkMemberInjectedInto(method, instance);
                }
            }
        }
Esempio n. 3
0
        internal object[] ResolveMultiple(CycleCounter cycleCounter, Type contractType)
        {
            var contractIsArray = contractType.IsArray;
            var elementType     = contractIsArray ? contractType.GetElementType() : contractType;
            var instances       = ResolveAll(cycleCounter, elementType);

            return(instances);
        }
Esempio n. 4
0
        private InstanceWrapper[] CreateInstances(CycleCounter cycleCounter, Type[] types, object[] args)
        {
            var typesLength = types.Length;
            var result      = new InstanceWrapper[typesLength];

            for (var i = 0; i < typesLength; i++)
            {
                var wrapper = CreateInstance(cycleCounter, types[i], args);
                result[i] = wrapper;
            }

            return(result);
        }
Esempio n. 5
0
        internal void InjectIntoInstance(CycleCounter cycleCounter, object instance)
        {
            if (injectedIntoInstances.Add(instance))
            {
                cycleCounter.Indent();

                ResolveFields(cycleCounter, instance);
                ResolveProperties(cycleCounter, instance);
                ResolveMethods(cycleCounter, instance);

                cycleCounter.Unindent();
            }
        }
Esempio n. 6
0
        private InstanceWrapper CreateInstance(CycleCounter cycleCounter, Type type, object[] args)
        {
            cycleCounter.Indent();

            var suitableConstructor = GetConstructor(type);
            var resolvedArgs        = ResolveArgs(cycleCounter, suitableConstructor, args);
            var instance            = activator.CreateInstance(suitableConstructor, resolvedArgs);

            cycleCounter.Unindent();

            InjectIntoInstance(cycleCounter, instance);

            return(new InstanceWrapper(instance));
        }
Esempio n. 7
0
        internal object Resolve(CycleCounter cycleCounter, Type contractType, params object[] args)
        {
            var contractIsArray = contractType.IsArray;
            var elementType     = contractIsArray ? contractType.GetElementType() : contractType;

            var instances = ResolveAll(cycleCounter, elementType, args);

            if (contractIsArray)
            {
                return(instances);
            }

            return(instances[0]);
        }
Esempio n. 8
0
        internal object[] ResolveAll(CycleCounter cycleCounter, Type contractType, params object[] args)
        {
            cycleCounter.LogType(contractType);

            if (!bindings.TryGetValue(contractType, out var resolver))
            {
                throw new TypeNotRegisteredException(contractType, $"Resolving graph:{Environment.NewLine}{cycleCounter.Pop()}{Environment.NewLine}{Environment.NewLine}" +
                                                     $"Bindings:{Environment.NewLine}{GetBindingsString(bindings)}");
            }

            var instances = resolver.GetInstances(cycleCounter, args);

            InitializeInstances(instances);

            return(instances.Select(instance => instance.Value).ToArray());
        }
Esempio n. 9
0
        private void ResolveProperties(CycleCounter cycleCounter, object instance)
        {
            var type                 = instance.GetType();
            var properties           = type.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
            var injectableProperties = properties.Where(property => property.GetCustomAttributes(container.injectAttributeType).Any()).ToArray();

            foreach (var property in injectableProperties)
            {
                if (CheckNeedsInjectIntoMember(property, instance))
                {
                    var values    = container.ResolveMultiple(cycleCounter, property.PropertyType);
                    var collected = CollectValue(property.PropertyType, values);
                    var value     = collected.Value;

                    property.SetValue(instance, value);

                    MarkMemberInjectedInto(property, instance);
                }
            }
        }
Esempio n. 10
0
        private void ResolveFields(CycleCounter cycleCounter, object instance)
        {
            var type             = instance.GetType();
            var fields           = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
            var injectableFields = fields.Where(field => field.GetCustomAttributes(container.injectAttributeType).Any()).ToArray();

            foreach (var field in injectableFields)
            {
                if (CheckNeedsInjectIntoMember(field, instance))
                {
                    var values    = container.ResolveMultiple(cycleCounter, field.FieldType);
                    var collected = CollectValue(field.FieldType, values);
                    var value     = collected.Value;

                    field.SetValue(instance, value);

                    MarkMemberInjectedInto(field, instance);
                }
            }
        }
Esempio n. 11
0
        private object[] ResolveArgs(CycleCounter cycleCounter, ConstructorInfo constructorInfo, object[] args)
        {
            var parameters = constructorInfo.GetParameters();

            if (parameters.Length == 0)
            {
                return(new object[0]);
            }

            var result = new object[parameters.Length];

            for (var i = 0; i < parameters.Length; i++)
            {
                var parameterInfo = parameters[i];
                var parameterType = parameterInfo.ParameterType;
                var lessArgs      = i >= args.Length;

                if (lessArgs)
                {
                    result[i] = container.Resolve(cycleCounter, parameterType);
                }
                else
                {
                    var arg = args[i];

                    if (CheckAssignable(parameterType, arg.GetType()))
                    {
                        result[i] = arg;
                    }
                    else
                    {
                        result[i] = container.Resolve(parameterType);
                    }
                }

                result = argConverter.Convert(parameterType, result);
            }

            return(result);
        }
Esempio n. 12
0
 internal object Resolve(CycleCounter cycleCounter, Type contractType)
 {
     return(Resolve(cycleCounter, contractType, new object[0]));
 }