VeldridCollection.cs 2.7 KB

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