Ejemplo n.º 1
0
        public void Inject(Type type, object instance, InjectionBehaviour injectionBehaviour)
        {
            if (!this.InjectedRegistrations.ContainsKey(type))
                this.InjectedRegistrations.Add(type, new List<Registration>());

            var registration = new Registration(type, instance?.GetType() ?? type, null, this.Lifecycle, injectionBehaviour);
            this.InjectedRegistrations[type].Add(registration);

            this.Insert(registration, type, instance);
        }
Ejemplo n.º 2
0
        public void Insert(Registration registration, Type type, object instance)
        {
            if(!this.Store.ContainsKey(type))
                this.Store.Add(type, new List<Tuple<Registration, object>>());

            if(registration.InjectionBehaviour == InjectionBehaviour.Override)
                this.Store[type].Clear();

            this.Store[type].Add(new Tuple<Registration, object>(registration, instance));
        }
Ejemplo n.º 3
0
        object GetInstance(Registration registration, IInstanceStore tempInstanceStore, Stack<Type> buildStack)
        {
            if (buildStack.Contains(registration.ConcreteType))
                throw new ContainerException("Cyclic dependency detected when trying to construct `" + registration.ConcreteType.AssemblyQualifiedName + "`", buildStack);

            buildStack.Push(registration.ConcreteType);

            var constructor = registration.Ctor ??
                               (container =>
                                    {
                                        var constructors = registration.ConcreteType.GetConstructors();
                                        var ctorsWithParams = constructors.Select(c => new {ctor = c, parameters = c.GetParameters()});
                                        var orderedEnumerable = ctorsWithParams.OrderBy(x => x.parameters.Length);
                                        foreach (var ctor in orderedEnumerable)
                                        {
                                            var parameterInfos = ctor.parameters.Select(p => p.ParameterType);

                                            this.CheckDependencies(registration.ConcreteType, parameterInfos, registration.Lifecycle, tempInstanceStore, buildStack);

                                            var parameters = new object[ctor.parameters.Length];
                                            for (var i = 0; i < ctor.parameters.Length; i++)
                                            {
                                                var newBuildStack = new Stack<Type>(buildStack.Reverse());
                                                if (ctor.parameters[i].ParameterType.IsGenericType && ctor.parameters[i].ParameterType.GetGenericTypeDefinition() == typeof (IEnumerable<>))
                                                {
                                                    var genericArgument = ctor.parameters[i].ParameterType.GetGenericArguments()[0];
                                                    parameters[i] = this.ResolveAll(genericArgument, newBuildStack);
                                                }
                                                else
                                                {
                                                    parameters[i] = this.Resolve(ctor.parameters[i].ParameterType, tempInstanceStore, newBuildStack);
                                                }
                                            }

                                            try
                                            {
                                                return ctor.ctor.Invoke(parameters);
                                            }
                                            catch(Exception e)
                                            {
                                                throw new ContainerException("Cannot create type `" + ctor.ctor.DeclaringType.FullName + "`", buildStack, e);
                                            }
                                        }

                                        throw new ContainerException("Unable to construct `" + registration.ConcreteType.AssemblyQualifiedName + "`", buildStack);
                                    });

            return constructor(this);
        }
Ejemplo n.º 4
0
 public void Insert(Registration registration, Type type, object instance)
 {
     throw new InvalidOperationException();
 }