private static Control InjectDependenciesRecursiveInternal(IApplicationContext appContext, Control control)
        {
            if (control is LiteralControl)
            {
                return(control);                           // nothing to do
            }
            ISupportsWebDependencyInjection diControl = control as ISupportsWebDependencyInjection;

            if (diControl != null && diControl.DefaultApplicationContext != null)
            {
                return(control); // nothing to do anymore - control cares for itself and its children
            }

            // "intercept" Control to make it DI-aware
            ControlInterceptor.EnsureControlIntercepted(appContext, control);

            // if the control is a UserControl, use ApplicationContext from it's physical location
            IApplicationContext appContextToUse = appContext;
            UserControl         userControl     = control as UserControl;

            if (userControl != null)
            {
                appContextToUse = GetControlApplicationContext(appContext, userControl);
            }

            // set ApplicationContext instance
            if (control is IApplicationContextAware)
            {
                ((IApplicationContextAware)control).ApplicationContext = appContextToUse;
            }

            // inject dependencies using control's context
            control = (Control)appContextToUse.ConfigureObject(control, control.GetType().FullName);

            // and now go for control's children
            if (control.HasControls())
            {
                ControlCollection childControls = control.Controls;
                int childCount = childControls.Count;
                for (int i = 0; i < childCount; i++)
                {
                    Control c = childControls[i];
                    if (c is LiteralControl)
                    {
                        continue;
                    }

                    Control configuredControl = InjectDependenciesRecursiveInternal(appContext, c);
                    if (configuredControl != c)
                    {
                        ControlAccessor ac = new ControlAccessor(c.Parent);
                        ac.SetControlAt(configuredControl, i);
                    }
                }
            }

            return(control);
        }
        private static bool IsDependencyInjectionAware(IApplicationContext defaultApplicationContext, object o)
        {
            ISupportsWebDependencyInjection diAware = o as ISupportsWebDependencyInjection;

            if (diAware != null)
            {
                // If the ControlCollection is alread DI-aware ensure appContext is set
                if (diAware.DefaultApplicationContext == null)
                {
                    diAware.DefaultApplicationContext = defaultApplicationContext;
                }
                return(true); // nothing more to do
            }
            return(false);
        }