Пример #1
0
        object Create(Type serviceTypeToCreate, ResolutionContext context)
        {
            var concreteTypeToCreate = GetConcreteType(serviceTypeToCreate, context);

            using (context.EnterResolutionContextOf(concreteTypeToCreate))
            {
                var constructors = concreteTypeToCreate
                                   .GetConstructors().FirstOrDefault();

                if (constructors == null)
                {
                    throw new InvalidOperationException(string.Format("Could not find valid constructor on {0}.", concreteTypeToCreate));
                }

                var parameters = constructors
                                 .GetParameters()
                                 .Select(p => Create(p.ParameterType, context))
                                 .ToArray();

                var instance = Activator.CreateInstance(concreteTypeToCreate, parameters);

                PerformCommissionTasks(instance);

                return(instance);
            }
        }