public static void Initialize() { if (_key == null) { _key = new SingletonKey(); } }
/// <summary> /// Unregisters a singleton instance of a type. /// </summary> /// <param name="Object">Singleton object instance.</param> /// <param name="Arguments">Any constructor arguments associated with the object instance.</param> /// <returns>If the instance was found and removed.</returns> public static bool Unregister(object Object, params object[] Arguments) { SingletonKey Key = new SingletonKey(Object.GetType(), Arguments); lock (instances) { return(instances.Remove(Key)); } }
/// <summary> /// Checks if a singleton type (with optional associated arguments) is registered. /// </summary> /// <param name="Type">Singleton type</param> /// <param name="Arguments">Any constructor arguments associated with the type.</param> /// <returns>If such a singleton type is registered.</returns> public static bool IsRegistered(Type Type, params object[] Arguments) { SingletonKey Key = new SingletonKey(Type, Arguments); lock (instances) { return(instances.ContainsKey(Key)); } }
public static T Get <T>(object key, bool throwExceptionIfNotFind = false) { var res = Get(SingletonKey.GetKey <T>(key)); if (res != null) { return((T)res); } if (throwExceptionIfNotFind) { throw new KeyNotFoundException("No se ha encontrado el objecto de tipo '" + typeof(T).Name + " con la clave '" + (key ?? "null") + "'."); } return(default(T)); }
/// <summary> /// Registers a singleton instance of a type. /// </summary> /// <param name="Object">Singleton objcet instance.</param> /// <param name="Arguments">Any constructor arguments associated with the object instance.</param> public static void Register(object Object, params object[] Arguments) { SingletonKey Key = new SingletonKey(Object.GetType(), Arguments); lock (instances) { if (instances.ContainsKey(Key)) { throw new InvalidOperationException("Singleton already registered."); } instances[Key] = Object; } }
private static void Add <T>(T objectInstance, SingletonKey key) { if (Instance.objects.ContainsKey(key)) { throw new ArgumentException("No se puede agregar el objeto porque la combinación clave/tipo esta en uso"); } Instance.objects.Add(key, objectInstance); foreach (var sub in Instance.subscriptions) { if (sub.Type == ((object)objectInstance).GetType() && sub.Key == key) { sub.Invoke(default(T), objectInstance, SingletonAction.Add); } } }
//public static bool Set(Type type, object key, object substitute) { return Set(SingletonKey.GetKey(type, key), substitute); } private static bool Set <T>(SingletonKey key, T substitute) { if (Instance.objects.ContainsKey(key)) { var previous = (T)Instance.objects[key]; Instance.objects[key] = substitute; foreach (var sub in Instance.subscriptions) { if (sub.Type == substitute.GetType() && sub.Key == key) { sub.Invoke(previous, substitute, SingletonAction.Set); } } return(true); } return(false); }
/// <summary> /// Returns an instance of the type <paramref name="Type"/>. /// </summary> /// <param name="Type">Type of objects to return.</param> /// <param name="Arguments">Constructor arguments.</param> /// <returns>Instance of <paramref name="Type"/>.</returns> public object Instantiate(Type Type, params object[] Arguments) { SingletonKey Key = new SingletonKey(Type, Arguments); lock (instances) { if (instances.TryGetValue(Key, out object Object)) { return(Object); } Object = Activator.CreateInstance(Type, Arguments); instances[Key] = Object; return(Object); } }
private static bool Remove <T>(SingletonKey key) { if (!Instance.objects.ContainsKey(key)) { return(false); } var obj = Instance.objects[key]; var res = Instance.objects.Remove(key); if (!res) { return(false); } foreach (var sub in Instance.subscriptions.Where(sub => sub.Type == obj.GetType() && sub.Key == key)) { sub.Invoke(obj, default(T), SingletonAction.Remove); } return(true); }
/// <summary> /// Returns an instance of the type <paramref name="Type"/>. /// </summary> /// <param name="ReturnNullIfFail">If null should be returned instead for throwing exceptions.</param> /// <param name="Type">Type of objects to return.</param> /// <param name="Arguments">Constructor arguments.</param> /// <returns>Instance of <paramref name="Type"/>.</returns> public object Instantiate(bool ReturnNullIfFail, Type Type, params object[] Arguments) { SingletonKey Key = new SingletonKey(Type, Arguments); object Object; lock (instances) { if (instances.TryGetValue(Key, out Object)) { return(Object); } } Object = Types.Create(ReturnNullIfFail, Type, Arguments); if (Object is null) { return(null); } lock (instances) { if (instances.TryGetValue(Key, out object Object2)) { if (Object is IDisposable Disposable) { Disposable.Dispose(); } return(Object2); } instances[Key] = Object; } return(Object); }
private static object Get(SingletonKey key) { return(Instance.objects.ContainsKey(key) ? Instance.objects[key] : null); }
protected MySingleton(SingletonKey key) : base(key) { }
public static bool Set <T>(T substitute) { return(Set <T>(SingletonKey.GetKey <T>(), substitute)); }
protected Singleton(SingletonKey key) { }
public static bool Contains <T>() { return(Contains <T>(SingletonKey.GetKey <T>())); }
private static bool Contains <T>(SingletonKey key) { return(Instance.objects.ContainsKey(key)); }
public static void Add <T>(T objectInstance, object key) { Add(objectInstance, SingletonKey.GetKey <T>(key)); }
public static void Add <T>() where T : new() { Add(new T(), SingletonKey.GetKey <T>()); }
private static void Subscribe <T>(Action <SingletonSubscriptionArgs <T> > changeAction, SingletonKey key, bool raiseAddIfAlreadyAdded = true) { Instance.subscriptions.Add(new SubscriptionItem { Type = typeof(T), Key = key, Action = changeAction }); if (raiseAddIfAlreadyAdded && Instance.objects.ContainsKey(key)) { changeAction(new SingletonSubscriptionArgs <T>(default(T), (T)Instance.objects[key], SingletonAction.Add)); } }
public static void Subscribe <T>(Action <SingletonSubscriptionArgs <T> > changeAction, object key, bool raiseAddIfAlreadyAdded = true) { Subscribe <T>(changeAction, SingletonKey.GetKey <T>(key)); }
public static bool Remove <T>(object key) { return(Remove <T>(SingletonKey.GetKey <T>(key))); }
public static bool Set <T>(T substitute, object key) { return(Set <T>(SingletonKey.GetKey <T>(key), substitute)); }
public static bool Remove <T>() { return(Remove <T>(SingletonKey.GetKey <T>())); }
public static bool Contains <T>(object key) { return(Contains <T>(SingletonKey.GetKey <T>(key))); }