private static CreateControlCollectionDelegate GetInterceptedCollectionFactory(Type ownerType, Type collectionType)
        {
            AssertUtils.State(typeof(Control).IsAssignableFrom(ownerType), "ownerType must be of type Control");
            AssertUtils.State(typeof(ControlCollection).IsAssignableFrom(collectionType), "collectionType must be of type ControlCollection");

            CreateControlCollectionDelegate factoryMethod = (CreateControlCollectionDelegate)s_collectionFactoryCache[ownerType];

            if (factoryMethod == null)
            {
                lock (s_collectionFactoryCache)
                {
                    factoryMethod = (CreateControlCollectionDelegate)s_collectionFactoryCache[ownerType];
                    if (factoryMethod == null)
                    {
                        Type interceptedCollectionType = GetInterceptedCollectionType(
                            collectionType
                            , WebDependencyInjectionUtils.InjectDependenciesRecursive
                            );

                        ConstructorInfo ctor = interceptedCollectionType.GetConstructor(new Type[] { typeof(Control) });
                        DynamicMethod   dm   = new DynamicMethod(string.Empty, typeof(ControlCollection), new Type[] { typeof(Control) });
                        ILGenerator     il   = dm.GetILGenerator();
                        il.Emit(OpCodes.Ldarg_0);
                        il.Emit(OpCodes.Newobj, ctor);
                        il.Emit(OpCodes.Ret);
                        factoryMethod = (CreateControlCollectionDelegate)dm.CreateDelegate(typeof(CreateControlCollectionDelegate));
                        s_collectionFactoryCache[ownerType] = factoryMethod;
                    }
                }
            }
            return(factoryMethod);
        }
Ejemplo n.º 2
0
        static ControlAccessor()
        {
            SafeField fldControls = null;
            MethodInfo fnClear = null;

            SecurityCritical.ExecutePrivileged(new PermissionSet(PermissionState.Unrestricted), delegate
            {
                fnClear = GetMethod("Clear");
                fldControls = new SafeField(typeof(ControlCollection).GetField("_controls", BindingFlags.Instance | BindingFlags.NonPublic));
            });

            s_miClear = fnClear;
            ControlsArrayField = fldControls;

            CreateControlCollectionDelegate fnBaseCreateControlCollection = null;
            AddedControlDelegate fnBaseAddedControl = null;
            RemovedControlDelegate fnBaseRemovedControl = null;
            VoidMethodDelegate fnBaseClearNamingContainer = null;

            SecurityCritical.ExecutePrivileged(new PermissionSet(PermissionState.Unrestricted), delegate
            {
                fnBaseCreateControlCollection = (CreateControlCollectionDelegate)Delegate.CreateDelegate(typeof(CreateControlCollectionDelegate), GetMethod("CreateControlCollection"));
                fnBaseAddedControl = (AddedControlDelegate)Delegate.CreateDelegate(typeof(AddedControlDelegate), GetMethod("AddedControl"));
                fnBaseRemovedControl = (RemovedControlDelegate)Delegate.CreateDelegate(typeof(RemovedControlDelegate), GetMethod("RemovedControl"));
                fnBaseClearNamingContainer = (VoidMethodDelegate)Delegate.CreateDelegate(typeof(VoidMethodDelegate), GetMethod("ClearNamingContainer"));
            });

            BaseCreateControlCollection = fnBaseCreateControlCollection;
            BaseAddedControl = fnBaseAddedControl;
            BaseRemovedControl = fnBaseRemovedControl;
            BaseClearNamingContainer = fnBaseClearNamingContainer;
        }
Ejemplo n.º 3
0
        static ControlAccessor()
        {
            SafeField  fldControls = null;
            MethodInfo fnClear     = null;

            SecurityCritical.ExecutePrivileged(new PermissionSet(PermissionState.Unrestricted), delegate
            {
                fnClear     = GetMethod("Clear");
                fldControls = new SafeField(typeof(ControlCollection).GetField("_controls", BindingFlags.Instance | BindingFlags.NonPublic));
            });

            s_miClear          = fnClear;
            ControlsArrayField = fldControls;

            CreateControlCollectionDelegate fnBaseCreateControlCollection = null;
            AddedControlDelegate            fnBaseAddedControl            = null;
            RemovedControlDelegate          fnBaseRemovedControl          = null;
            VoidMethodDelegate fnBaseClearNamingContainer = null;

            SecurityCritical.ExecutePrivileged(new PermissionSet(PermissionState.Unrestricted), delegate
            {
                fnBaseCreateControlCollection = (CreateControlCollectionDelegate)Delegate.CreateDelegate(typeof(CreateControlCollectionDelegate), GetMethod("CreateControlCollection"));
                fnBaseAddedControl            = (AddedControlDelegate)Delegate.CreateDelegate(typeof(AddedControlDelegate), GetMethod("AddedControl"));
                fnBaseRemovedControl          = (RemovedControlDelegate)Delegate.CreateDelegate(typeof(RemovedControlDelegate), GetMethod("RemovedControl"));
                fnBaseClearNamingContainer    = (VoidMethodDelegate)Delegate.CreateDelegate(typeof(VoidMethodDelegate), GetMethod("ClearNamingContainer"));
            });

            BaseCreateControlCollection = fnBaseCreateControlCollection;
            BaseAddedControl            = fnBaseAddedControl;
            BaseRemovedControl          = fnBaseRemovedControl;
            BaseClearNamingContainer    = fnBaseClearNamingContainer;
        }
        private static ControlCollection InterceptCollection(Control owner, ControlCollection originalCollection)
        {
            CreateControlCollectionDelegate factoryMethod = GetInterceptedCollectionFactory(owner.GetType(), originalCollection.GetType());
            ControlCollection interceptedCollection       = factoryMethod(owner);

            ReflectionUtils.MemberwiseCopy(originalCollection, interceptedCollection);
            return(interceptedCollection);
        }
        internal static ControlCollection TryCreateCollection(Control owner)
        {
            CreateControlCollectionDelegate factoryMethod = (CreateControlCollectionDelegate)s_collectionFactoryCache[owner.GetType()];

            if (factoryMethod != null)
            {
                return(factoryMethod(owner));
            }
            return(null);
        }
        private static CreateControlCollectionDelegate GetInterceptedCollectionFactory(Type controlType, Type controlCollectionType)
        {
            AssertUtils.State(typeof(Control).IsAssignableFrom(controlType), "controlType must be of type Control");
            AssertUtils.State(typeof(ControlCollection).IsAssignableFrom(controlCollectionType), "controlCollectionType must be of type ControlCollection");

            CreateControlCollectionDelegate factoryMethod = (CreateControlCollectionDelegate)s_collectionFactoryCache[controlType];

            if (factoryMethod == null)
            {
                lock (s_collectionFactoryCache)
                {
                    factoryMethod = (CreateControlCollectionDelegate)s_collectionFactoryCache[controlType];
                    if (factoryMethod == null)
                    {
                        Type interceptedCollectionType = GetInterceptedCollectionType(controlCollectionType, new InjectDependenciesCallbackHandler(WebDependencyInjectionUtils.InjectDependenciesRecursive));

                        ConstructorInfo     ctor    = interceptedCollectionType.GetConstructor(new Type[] { typeof(Control) });
                        IDynamicConstructor dynCtor = new SafeConstructor(ctor);
                        s_collectionFactoryCache[controlType] = new CreateControlCollectionDelegate(new CreateControlCollectionWrapper(dynCtor).Create);
                    }
                }
            }
            return(factoryMethod);
        }
        private static CreateControlCollectionDelegate GetInterceptedCollectionFactory(Type controlType, Type controlCollectionType)
        {
            AssertUtils.State( typeof(Control).IsAssignableFrom(controlType), "controlType must be of type Control" );
            AssertUtils.State( typeof(ControlCollection).IsAssignableFrom(controlCollectionType), "controlCollectionType must be of type ControlCollection" );

            CreateControlCollectionDelegate factoryMethod = (CreateControlCollectionDelegate)s_collectionFactoryCache[controlType];
            if (factoryMethod == null)
            {
                lock (s_collectionFactoryCache)
                {
                    factoryMethod = (CreateControlCollectionDelegate)s_collectionFactoryCache[controlType];
                    if (factoryMethod == null)
                    {
                        Type interceptedCollectionType = GetInterceptedCollectionType(controlCollectionType, new InjectDependenciesCallbackHandler(WebDependencyInjectionUtils.InjectDependenciesRecursive));

                        ConstructorInfo ctor = interceptedCollectionType.GetConstructor(new Type[] {typeof (Control)});
                        IDynamicConstructor dynCtor = new SafeConstructor(ctor);
                        s_collectionFactoryCache[controlType] = new CreateControlCollectionDelegate(new CreateControlCollectionWrapper(dynCtor).Create);
                    }
                }
            }
            return factoryMethod;
        }