private IWebObjectFactory GetAdapterFactory(Type adapterType) {
            if (_controlAdapterFactoryGenerator == null) {
                lock (_staticLock) {
                    if (_controlAdapterFactoryGenerator == null) {
                        _controlAdapterFactoryTable = new Hashtable();
                        _controlAdapterFactoryGenerator = new FactoryGenerator();
                    }
                }
            }

            IWebObjectFactory factory = (IWebObjectFactory)_controlAdapterFactoryTable[adapterType];
            if (factory == null) {

                lock (_controlAdapterFactoryTable.SyncRoot) {
                    factory = (IWebObjectFactory)_controlAdapterFactoryTable[adapterType];

                    if (factory == null) {
                        try {
                            factory = _controlAdapterFactoryGenerator.CreateFactory(adapterType);
                        }
                        catch {
                            throw new Exception(SR.GetString(SR.Could_not_create_type_instance, adapterType.ToString()));
                        }

                        _controlAdapterFactoryTable[adapterType] = factory;
                    }
                }
            }

            return factory;
        }
 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();
 }
        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();
        }
예제 #4
0
 internal static object FastCreatePublicInstance(Type type)
 {
   if (!type.Assembly.GlobalAssemblyCache)
     return HttpRuntime.CreatePublicInstance(type);
   if (!HttpRuntime.s_initializedFactory)
   {
     lock (HttpRuntime.s_factoryLock)
     {
       if (!HttpRuntime.s_initializedFactory)
       {
         HttpRuntime.s_factoryGenerator = new FactoryGenerator();
         HttpRuntime.s_factoryCache = Hashtable.Synchronized(new Hashtable());
         HttpRuntime.s_initializedFactory = true;
       }
     }
   }
   IWebObjectFactory webObjectFactory = (IWebObjectFactory) HttpRuntime.s_factoryCache[(object) type];
   if (webObjectFactory == null)
   {
     webObjectFactory = HttpRuntime.s_factoryGenerator.CreateFactory(type);
     HttpRuntime.s_factoryCache[(object) type] = (object) webObjectFactory;
   }
   return webObjectFactory.CreateInstance();
 }