コード例 #1
0
        private Control LoadControl(IWebObjectFactory objectFactory, System.Web.VirtualPath virtualPath, Type t, object[] parameters)
        {
            BuildResultCompiledType         type    = null;
            BuildResultNoCompileUserControl control = null;
            PartialCachingAttribute         cachingAttribute;

            if (objectFactory != null)
            {
                type = objectFactory as BuildResultCompiledType;
                if (type != null)
                {
                    t = type.ResultType;
                    Util.CheckAssignableType(typeof(UserControl), t);
                }
                else
                {
                    control = (BuildResultNoCompileUserControl)objectFactory;
                }
            }
            else if (t != null)
            {
                Util.CheckAssignableType(typeof(Control), t);
            }
            if (t != null)
            {
                cachingAttribute = (PartialCachingAttribute)TypeDescriptor.GetAttributes(t)[typeof(PartialCachingAttribute)];
            }
            else
            {
                cachingAttribute = control.CachingAttribute;
            }
            if (cachingAttribute == null)
            {
                Control control2;
                if (objectFactory != null)
                {
                    control2 = (Control)objectFactory.CreateInstance();
                }
                else
                {
                    control2 = (Control)HttpRuntime.CreatePublicInstance(t, parameters);
                }
                UserControl control3 = control2 as UserControl;
                if (control3 != null)
                {
                    if (virtualPath != null)
                    {
                        control3.TemplateControlVirtualPath = virtualPath;
                    }
                    control3.InitializeAsUserControl(this.Page);
                }
                return(control2);
            }
            HashCodeCombiner combinedHashCode = new HashCodeCombiner();

            if (objectFactory != null)
            {
                combinedHashCode.AddObject(objectFactory);
            }
            else
            {
                combinedHashCode.AddObject(t);
            }
            if (!cachingAttribute.Shared)
            {
                this.AddStackContextToHashCode(combinedHashCode);
            }
            string combinedHashString = combinedHashCode.CombinedHashString;

            return(new PartialCachingControl(objectFactory, t, cachingAttribute, "_" + combinedHashString, parameters));
        }
コード例 #2
0
        private Control LoadControl(IWebObjectFactory objectFactory, VirtualPath virtualPath, Type t, object[] parameters)
        {
            // Make sure we get an object factory or a type, but not both
            Debug.Assert((objectFactory == null) != (t == null));

            BuildResultCompiledType         compiledUCResult  = null;
            BuildResultNoCompileUserControl noCompileUCResult = null;

            if (objectFactory != null)
            {
                // It can be a compiled or no-compile user control
                compiledUCResult = objectFactory as BuildResultCompiledType;
                if (compiledUCResult != null)
                {
                    t = compiledUCResult.ResultType;
                    Debug.Assert(t != null);

                    // Make sure it's a user control (VSWhidbey 428718)
                    Util.CheckAssignableType(typeof(UserControl), t);
                }
                else
                {
                    noCompileUCResult = (BuildResultNoCompileUserControl)objectFactory;
                    Debug.Assert(noCompileUCResult != null);
                }
            }
            else
            {
                // Make sure the type has the correct base class (ASURT 123677)
                if (t != null)
                {
                    Util.CheckAssignableType(typeof(Control), t);
                }
            }

            PartialCachingAttribute cacheAttrib;

            // Check if the user control has a PartialCachingAttribute attribute
            if (t != null)
            {
                cacheAttrib = (PartialCachingAttribute)
                              TypeDescriptor.GetAttributes(t)[typeof(PartialCachingAttribute)];
            }
            else
            {
                cacheAttrib = noCompileUCResult.CachingAttribute;
            }

            if (cacheAttrib == null)
            {
                // The control is not cached.  Just create it.
                Control c;
                if (objectFactory != null)
                {
                    c = (Control)objectFactory.CreateInstance();
                }
                else
                {
                    c = (Control)HttpRuntime.CreatePublicInstance(t, parameters);
                }

                // If it's a user control, do some extra initialization
                UserControl uc = c as UserControl;
                if (uc != null)
                {
                    Debug.Assert(virtualPath != null);
                    if (virtualPath != null)
                    {
                        uc.TemplateControlVirtualPath = virtualPath;
                    }
                    uc.InitializeAsUserControl(Page);
                }

                return(c);
            }

            HashCodeCombiner combinedHashCode = new HashCodeCombiner();

            // Start by adding the type or object factory of the user control to the hash.
            // This guarantees that two unrelated user controls don't share the same cached data.
            if (objectFactory != null)
            {
                combinedHashCode.AddObject(objectFactory);
            }
            else
            {
                combinedHashCode.AddObject(t);
            }

            // If it's not shared, add some stack frames to the hash
            if (!cacheAttrib.Shared)
            {
                AddStackContextToHashCode(combinedHashCode);
            }

            string cacheKey = combinedHashCode.CombinedHashString;

            // Wrap it to allow it to be cached
            return(new PartialCachingControl(objectFactory, t, cacheAttrib, "_" + cacheKey, parameters));
        }