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