VeldridCollection.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Runtime.CompilerServices;
  5. using System.Text;
  6. using Veldrid.Common.Tools;
  7. namespace Veldrid.Common
  8. {
  9. public class VeldridCollection<T> : ICollection<T>,IDisposable where T : class
  10. {
  11. internal event EventHandler<T> ItemAddEvent;
  12. internal event EventHandler ItemRemoveEvent;
  13. private List<T> _list = new List<T>();
  14. private bool disposedValue;
  15. public int Count =>_list.Count;
  16. public bool IsReadOnly => false;
  17. public T this[int index] { get => _list[index]; set => _list[index] = value; }
  18. public void Add(T item)
  19. {
  20. if(!_list.Contains(item))
  21. {
  22. _list.Add(item);
  23. ItemAddEvent?.Invoke(this, item);
  24. }
  25. }
  26. public void Clear()
  27. {
  28. _list.ForEach(x => x.ClearEventHandle());
  29. _list.Clear();
  30. ItemRemoveEvent?.Invoke(this, EventArgs.Empty);
  31. }
  32. public bool Contains(T item)
  33. {
  34. return _list.Contains(item);
  35. }
  36. public void CopyTo(T[] array, int arrayIndex)
  37. {
  38. _list.CopyTo(array, arrayIndex);
  39. }
  40. public IEnumerator<T> GetEnumerator()
  41. {
  42. return _list.GetEnumerator();
  43. }
  44. public bool Remove(T item)
  45. {
  46. Boolean result = _list.Remove(item);
  47. item?.ClearEventHandle();
  48. if (result)ItemRemoveEvent?.Invoke(this, EventArgs.Empty);
  49. return result;
  50. }
  51. IEnumerator IEnumerable.GetEnumerator()
  52. {
  53. return _list.GetEnumerator();
  54. }
  55. public List<T> ToList() => _list;
  56. public T[] ToArray() => _list.ToArray();
  57. public void ForEach(Action<T> action) => _list.ForEach(action);
  58. protected virtual void Dispose(bool disposing)
  59. {
  60. if (!disposedValue)
  61. {
  62. if (disposing)
  63. {
  64. Clear();
  65. this.ClearEventHandle();
  66. // TODO: 释放托管状态(托管对象)
  67. }
  68. // TODO: 释放未托管的资源(未托管的对象)并重写终结器
  69. // TODO: 将大型字段设置为 null
  70. disposedValue = true;
  71. }
  72. }
  73. // // TODO: 仅当“Dispose(bool disposing)”拥有用于释放未托管资源的代码时才替代终结器
  74. // ~VeldridCollection()
  75. // {
  76. // // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
  77. // Dispose(disposing: false);
  78. // }
  79. public void Dispose()
  80. {
  81. // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
  82. Dispose(disposing: true);
  83. GC.SuppressFinalize(this);
  84. }
  85. }
  86. }