public T Add(Type type, IServiceProvider serviceProvider)
        {
            (myCount != myObjects.Length).AssertTrue("Array of injectable objects is already full");

            InjectableObject <T> injectableObject = InjectableObject <T> .Create(type, serviceProvider);

            myObjects[myCount++] = injectableObject;

            return(injectableObject.Instance);
        }
        public Task VisitActivity <TActivity>(ActivityNode <TActivity> activityNode) where TActivity : class, IActivity
        {
            activityNode.AssertNotNull("activityNode != null");

            InjectableObject <TActivity> activityWrapper = InjectableObject <TActivity> .Create(ServiceProvider);

            Log.Info("At node: {0}. Activity created: {1}", activityNode, activityWrapper.Instance);

            try
            {
                TActivity activity = activityWrapper.Instance;

                activityNode.OnActivityCreated(activity);

                Log.Info("At node: {0}. Activity started", activityNode);

                Task <object> task = activity.Execute();

                if (task.IsCompleted)
                {
                    activityNode.OnActivityCompleted(task);

                    activityWrapper.Dispose();

                    return(ExecuteNextNode(activityNode, task));
                }

                Task <Task> continuation = task.ContinueWith(t =>
                {
                    activityNode.OnActivityCompleted(task);

                    // ReSharper disable once AccessToDisposedClosure
                    activityWrapper.Dispose();

                    return(ExecuteNextNode(activityNode, t));
                }, myScheduler);

                return(continuation.Unwrap());
            }
            catch (Exception)
            {
                activityWrapper.Dispose();
                throw;
            }
        }