Ejemplo n.º 1
0
        private static ControlBuilder CreateBuilderFromType(Type type) {
            // Create the factory generator on demand
            if (s_controlBuilderFactoryCache == null) {
#if !DONTUSEFACTORYGENERATOR
                s_controlBuilderFactoryGenerator = new FactoryGenerator();
#endif // DONTUSEFACTORYGENERATOR

                // Create the factory cache
                s_controlBuilderFactoryCache = Hashtable.Synchronized(new Hashtable());

                // Seed the cache with a few types that we don't want to expose as public (they
                // need to be public for FactoryGenerator to be used).
                s_controlBuilderFactoryCache[typeof(Content)] = new ContentBuilderInternalFactory();
                s_controlBuilderFactoryCache[typeof(ContentPlaceHolder)] = new ContentPlaceHolderBuilderFactory();
            }

            // First, check if it's cached
            IWebObjectFactory factory = (IWebObjectFactory)s_controlBuilderFactoryCache[type];

            if (factory == null) {
                // Check whether the control's class exposes a custom builder type
                ControlBuilderAttribute cba = GetControlBuilderAttribute(type);

                if (cba != null) {
                    // Make sure the type has the correct base class (ASURT 123677)
                    Util.CheckAssignableType(typeof(ControlBuilder), cba.BuilderType);
#if !DONTUSEFACTORYGENERATOR
                    if (cba.BuilderType.IsPublic) {
                        // If the builder type is public, codegen a fast factory for it
                        factory = s_controlBuilderFactoryGenerator.CreateFactory(cba.BuilderType);
                    }
                    else {
                        Debug.Assert(false, "The type " + cba.BuilderType.Name + " should be made public for better performance.");
#endif // DONTUSEFACTORYGENERATOR

                        // It's not public, so we must stick with slower reflection
                        factory = new ReflectionBasedControlBuilderFactory(cba.BuilderType);
#if !DONTUSEFACTORYGENERATOR
                    }
#endif // DONTUSEFACTORYGENERATOR
                }
                else {
                    // use a factory that creates generic builders (i.e. ControlBuilder's)
                    factory = s_defaultControlBuilderFactory;
                }

                // Cache the factory
                s_controlBuilderFactoryCache[type] = factory;
            }

            return (ControlBuilder) factory.CreateInstance();
        }
 private static ControlBuilder CreateBuilderFromType(Type type)
 {
     if (s_controlBuilderFactoryCache == null)
     {
         s_controlBuilderFactoryGenerator = new FactoryGenerator();
         s_controlBuilderFactoryCache = Hashtable.Synchronized(new Hashtable());
         s_controlBuilderFactoryCache[typeof(Content)] = new ContentBuilderInternalFactory();
         s_controlBuilderFactoryCache[typeof(ContentPlaceHolder)] = new ContentPlaceHolderBuilderFactory();
     }
     IWebObjectFactory factory = (IWebObjectFactory) s_controlBuilderFactoryCache[type];
     if (factory == null)
     {
         ControlBuilderAttribute controlBuilderAttribute = GetControlBuilderAttribute(type);
         if (controlBuilderAttribute != null)
         {
             System.Web.UI.Util.CheckAssignableType(typeof(ControlBuilder), controlBuilderAttribute.BuilderType);
             if (controlBuilderAttribute.BuilderType.IsPublic)
             {
                 factory = s_controlBuilderFactoryGenerator.CreateFactory(controlBuilderAttribute.BuilderType);
             }
             else
             {
                 factory = new ReflectionBasedControlBuilderFactory(controlBuilderAttribute.BuilderType);
             }
         }
         else
         {
             factory = s_defaultControlBuilderFactory;
         }
         s_controlBuilderFactoryCache[type] = factory;
     }
     return (ControlBuilder) factory.CreateInstance();
 }