/// <summary>
        /// Intercepts the given <see cref="ControlCollection"/> by dynamically deriving
        /// the original type and let it implement <see cref="ISupportsWebDependencyInjection"/>.
        /// </summary>
        /// <param name="defaultApplicationContext">the ApplicationContext to be set on the collection instance.</param>
        /// <param name="ctlAccessor">a wrapper around the owner control instance.</param>
        /// <param name="ctlColAccessor">a wrapper around the collection instance.</param>
        /// <returns><value>true</value>, if interception was successful.  <value>false</value> otherwise</returns>
        public bool Intercept(IApplicationContext defaultApplicationContext,
                              ControlAccessor ctlAccessor, ControlCollectionAccessor ctlColAccessor)
        {
            Type collectionType = ctlColAccessor.GetTargetType();

            if (collectionType.IsSealed ||
                !ReflectionUtils.IsTypeVisible(collectionType, DynamicProxyManager.ASSEMBLY_NAME))
            //  || (null == collectionType.GetConstructor(new Type[] {typeof (Control)}))
            {
                return(false);
            }

            // this will enhance the collection's type and create a new instance of this type with fields copied from original collection
            try
            {
                ControlCollection childControls = InterceptCollection(ctlAccessor.GetTarget(), ctlColAccessor.GetTarget());
                ((ISupportsWebDependencyInjection)childControls).DefaultApplicationContext = defaultApplicationContext;
                ctlAccessor.Controls = childControls;
            }
            catch
            {
                // this may happen, if the ControlCollection doesn't contain a standard-ctor ControlCollection( Control owner)
                return(false);
            }
            return(true);
        }
        /// <summary>
        /// Intercepts the given <see cref="ControlCollection"/> by dynamically deriving 
        /// the original type and let it implement <see cref="ISupportsWebDependencyInjection"/>.
        /// </summary>
        /// <param name="defaultApplicationContext">the ApplicationContext to be set on the collection instance.</param>
        /// <param name="ctlAccessor">a wrapper around the owner control instance.</param>
        /// <param name="ctlColAccessor">a wrapper around the collection instance.</param>
        /// <returns><value>true</value>, if interception was successful.  <value>false</value> otherwise</returns>
        public bool Intercept(IApplicationContext defaultApplicationContext,
            ControlAccessor ctlAccessor, ControlCollectionAccessor ctlColAccessor)
        {
            Type collectionType = ctlColAccessor.GetTargetType();
            if (collectionType.IsSealed ||
                !ReflectionUtils.IsTypeVisible(collectionType, DynamicProxyManager.ASSEMBLY_NAME))
            //  || (null == collectionType.GetConstructor(new Type[] {typeof (Control)}))
            {
                return false;
            }

            // this will enhance the collection's type and create a new instance of this type with fields copied from original collection
            try
            {
                ControlCollection childControls = InterceptCollection(ctlAccessor.GetTarget(), ctlColAccessor.GetTarget() );
                ((ISupportsWebDependencyInjection)childControls).DefaultApplicationContext = defaultApplicationContext;
                ctlAccessor.Controls = childControls;
            }
            catch
            {
                // this may happen, if the ControlCollection doesn't contain a standard-ctor ControlCollection( Control owner)
                return false;
            }
            return true;
        }
 public bool Intercept(IApplicationContext defaultApplicationContext, ControlAccessor ctlAccessor,
                       ControlCollectionAccessor ctlColAccessor)
 {
     Control target = ctlAccessor.GetTarget();
     ctlColAccessor.Owner = target is INamingContainer 
         ? new NamingContainerSupportsWebDependencyInjectionOwnerProxy(defaultApplicationContext, target)
         : new SupportsWebDependencyInjectionOwnerProxy(defaultApplicationContext, target);
     return true;
 }
        public bool Intercept(IApplicationContext defaultApplicationContext, ControlAccessor ctlAccessor,
                              ControlCollectionAccessor ctlColAccessor)
        {
            Control target = ctlAccessor.GetTarget();

            ctlColAccessor.Owner = target is INamingContainer
                ? new NamingContainerSupportsWebDependencyInjectionOwnerProxy(defaultApplicationContext, target)
                : new SupportsWebDependencyInjectionOwnerProxy(defaultApplicationContext, target);
            return(true);
        }
        private static void EnsureControlCollectionIntercepted(IApplicationContext defaultApplicationContext, Control control)
        {
            // check the collection
            ControlAccessor   ctlAccessor   = new ControlAccessor(control);
            ControlCollection childControls = ctlAccessor.Controls;

            if (IsDependencyInjectionAware(defaultApplicationContext, childControls))
            {
                return; // nothing more to do
            }

            // check, if the collection's owner has already been intercepted
            ControlCollectionAccessor ctlColAccessor = new ControlCollectionAccessor(childControls);

            if (IsDependencyInjectionAware(defaultApplicationContext, ctlColAccessor.Owner))
            {
                return; // nothing more to do
            }

            // lookup strategy in cache
            IInterceptionStrategy strategy = null;

            lock (s_cachedInterceptionStrategies)
            {
                strategy = (IInterceptionStrategy)s_cachedInterceptionStrategies[control.GetType()];
            }

            if (strategy != null)
            {
                strategy.Intercept(defaultApplicationContext, ctlAccessor, ctlColAccessor);
            }
            else
            {
                // nothing in cache - try well-known strategies for owner resp. child collection type
                strategy = (IInterceptionStrategy)s_knownInterceptionStrategies[control.GetType()];
                if (strategy == null)
                {
                    strategy = (IInterceptionStrategy)s_knownInterceptionStrategies[childControls.GetType()];
                }

                // try intercept using well-known strategy
                if (strategy != null)
                {
                    bool bOk = strategy.Intercept(defaultApplicationContext, ctlAccessor, ctlColAccessor);
                    if (!bOk)
                    {
                        strategy = null;
                    }
                }

                // not well-known or didn't work out
                if (strategy == null)
                {
                    // probe for a strategy
                    bool bOk = false;
                    for (int i = 0; i < s_availableInterceptionStrategies.Length; i++)
                    {
                        strategy = s_availableInterceptionStrategies[i];
                        bOk      = strategy.Intercept(defaultApplicationContext, ctlAccessor, ctlColAccessor);
                        if (bOk)
                        {
                            break;
                        }
                    }
                    if (!bOk)
                    {
                        LogManager.GetLogger(typeof(ControlInterceptor)).Warn(string.Format("dependency injection not supported for control type {0}", ctlAccessor.GetTarget().GetType()));
                        strategy = s_noopInterceptionStrategy;
                    }
                }

                lock (s_cachedInterceptionStrategies)
                {
                    s_cachedInterceptionStrategies[control.GetType()] = strategy;
                }
            }
        }
        private static void EnsureControlCollectionIntercepted( IApplicationContext defaultApplicationContext, Control control )
        {
            // check the collection
            ControlAccessor ctlAccessor = new ControlAccessor( control );
            ControlCollection childControls = ctlAccessor.Controls;
            if (IsDependencyInjectionAware( defaultApplicationContext, childControls ))
            {
                return; // nothing more to do				
            }

            // check, if the collection's owner has already been intercepted
            ControlCollectionAccessor ctlColAccessor = new ControlCollectionAccessor( childControls );
            if (IsDependencyInjectionAware( defaultApplicationContext, ctlColAccessor.Owner ))
            {
                return; // nothing more to do				
            }

            // lookup strategy in cache
            IInterceptionStrategy strategy = null;
            lock (s_cachedInterceptionStrategies)
            {
                strategy = (IInterceptionStrategy)s_cachedInterceptionStrategies[control.GetType()];
            }

            if (strategy != null)
            {
                strategy.Intercept( defaultApplicationContext, ctlAccessor, ctlColAccessor );
            }
            else
            {
                // nothing in cache - try well-known strategies for owner resp. child collection type
                strategy = (IInterceptionStrategy)s_knownInterceptionStrategies[control.GetType()];
                if (strategy == null)
                {
                    strategy = (IInterceptionStrategy)s_knownInterceptionStrategies[childControls.GetType()];
                }

                // try intercept using well-known strategy
                if (strategy != null)
                {
                    bool bOk = strategy.Intercept( defaultApplicationContext, ctlAccessor, ctlColAccessor );
                    if (!bOk)
                    {
                        strategy = null;
                    }
                }

                // not well-known or didn't work out
                if (strategy == null)
                {
                    // probe for a strategy
                    bool bOk = false;
                    for (int i = 0; i < s_availableInterceptionStrategies.Length; i++)
                    {
                        strategy = s_availableInterceptionStrategies[i];
                        bOk = strategy.Intercept( defaultApplicationContext, ctlAccessor, ctlColAccessor );
                        if (bOk)
                            break;
                    }
                    if (!bOk)
                    {
                        LogManager.GetLogger( typeof( ControlInterceptor ) ).Warn( string.Format( "dependency injection not supported for control type {0}", ctlAccessor.GetTarget().GetType() ) );
                        strategy = s_noopInterceptionStrategy;
                    }
                }

                lock (s_cachedInterceptionStrategies)
                {
                    s_cachedInterceptionStrategies[control.GetType()] = strategy;
                }
            }
        }