public static WebProxyFactory GetInstance <T>() where T : WebProxyFactory { WebProxyFactory instance; try { instance = DoGetInstance(typeof(T)); } catch (Exception e) { Logger.GetInstance(typeof(WebProxyFactory)).Fatal("Instance initialization error: " + e); Logger.GetInstance(typeof(WebProxyFactory)).Info("Initializing " + typeof(DefaultWebProxyFactory).FullName + "..."); instance = new DefaultWebProxyFactory(); } return(instance); }
private static WebProxyFactory DoGetInstance(Type type) { if (type == null) { throw new ArgumentException("Invalid arguments to get " + typeof(WebProxyFactory).Name + " instance"); } var key = type.FullName + "_"; WebProxyFactory instance = null; if (Instances.ContainsKey(key)) { instance = Instances[key]; } if (instance == null) { Logger.GetInstance(typeof(WebProxyFactory)).Info("Initializing " + key + "..."); var constructor = type.GetConstructor(new Type[] { }); if (constructor != null) { instance = (WebProxyFactory)constructor.Invoke(new object[] { }); } } if (instance == null) { Logger.GetInstance(typeof(WebProxyFactory)).Info("Initializing " + typeof(DefaultWebProxyFactory).FullName + "..."); instance = new DefaultWebProxyFactory(); } lock (InstancesLock) { if (!Instances.ContainsKey(key)) { Instances.Add(key, instance); } } return(instance); }