Пример #1
0
        public static Config GetInstance <T>() where T : Config
        {
            Config instance;

            try
            {
                instance = DoGetInstance(typeof(T));
            }
            catch (Exception e)
            {
                Logger.GetInstance(typeof(Config)).Fatal("Instance initialization error: " + e);
                Logger.GetInstance(typeof(Config)).Info("Initializing " + typeof(AppSettingsConfig).FullName + "...");
                instance = new AppSettingsConfig();
            }
            return(instance);
        }
Пример #2
0
        private static Config DoGetInstance(Type type)
        {
            if (type == null)
            {
                throw new ArgumentException("Invalid arguments to get " + typeof(Config).Name + " instance");
            }

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

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