public void ForceRestore(T t) { var reset = t as IReset; reset?.Reset(); AvaliableObjects.Add(t); }
public void Clear() { RestoreAll(); foreach (var instance in AvaliableObjects) { if (instance != null) { destroy?.Invoke(instance); } } AvaliableObjects.Clear(); DispatchedObjects.Clear(); }
public bool Restore(T t) { // 必须是由该池创建的对象才允许被归还到池内。 if (t == null || !DispatchedObjects.Contains(t)) { return(false); } //T 可能为 C# 核心库类型,所以对 T 不做 IReset 限制 var reset = t as IReset; reset?.Reset(); DispatchedObjects.Remove(t); AvaliableObjects.Add(t); return(true); }
public T Take() { T instance; if (AvaliableObjects.Count == 0) { instance = createFunc(); onCreated?.Invoke(instance); } else { instance = AvaliableObjects[AvaliableObjects.Count - 1]; AvaliableObjects.Remove(instance); } if (DispatchedObjects == null) { DispatchedObjects = new List <T>(initCount); } DispatchedObjects.Add(instance); return(instance); }