Disposer.cs 705 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System;
  2. using MongoDB.Bson.Serialization.Attributes;
  3. namespace Model
  4. {
  5. [BsonKnownTypes(typeof(Entity))]
  6. [BsonKnownTypes(typeof(Component))]
  7. public abstract class Disposer : Object, IDisposable
  8. {
  9. [BsonIgnoreIfDefault]
  10. [BsonDefaultValue(1L)]
  11. [BsonElement]
  12. [BsonId]
  13. public long Id { get; set; }
  14. [BsonIgnore]
  15. public bool IsFromPool { get; set; }
  16. protected Disposer()
  17. {
  18. this.Id = IdGenerater.GenerateId();
  19. ObjectEvents.Instance.Add(this);
  20. }
  21. protected Disposer(long id)
  22. {
  23. this.Id = id;
  24. ObjectEvents.Instance.Add(this);
  25. }
  26. public virtual void Dispose()
  27. {
  28. this.Id = 0;
  29. if (this.IsFromPool)
  30. {
  31. ObjectPool.Instance.Recycle(this);
  32. }
  33. }
  34. }
  35. }