コード例 #1
0
        public static PreferenceFactory GetInstance <T>() where T : PreferenceFactory
        {
            PreferenceFactory instance;

            try
            {
                instance = DoGetInstance(typeof(T));
            }
            catch (Exception e)
            {
                Logger.GetInstance(typeof(PreferenceFactory)).Fatal("Instance initialization error: " + e);
                Logger.GetInstance(typeof(PreferenceFactory)).Info("Initializing " + typeof(DefaultPreferenceFactory).FullName + "...");
                instance = new DefaultPreferenceFactory();
            }
            return(instance);
        }
コード例 #2
0
        private static PreferenceFactory DoGetInstance(Type type)
        {
            if (type == null)
            {
                throw new ArgumentException("Invalid arguments to get " + typeof(PreferenceFactory).Name + " instance");
            }

            var key = type.FullName + "_";
            PreferenceFactory instance = null;

            if (Instances.ContainsKey(key))
            {
                instance = Instances[key];
            }
            if (instance == null)
            {
                Logger.GetInstance(typeof(PreferenceFactory)).Info("Initializing " + key + "...");
                var constructor = type.GetConstructor(new Type[] { });
                if (constructor != null)
                {
                    instance = (PreferenceFactory)constructor.Invoke(new object[] { });
                }
            }
            if (instance == null)
            {
                Logger.GetInstance(typeof(PreferenceFactory)).Info("Initializing " + typeof(DefaultPreferenceFactory).FullName + "...");
                instance = new DefaultPreferenceFactory();
            }
            lock (InstancesLock)
            {
                if (!Instances.ContainsKey(key))
                {
                    Instances.Add(key, instance);
                }
            }
            return(instance);
        }