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