diff --git a/GFramework.Core/Extensions/ArrayPoolExtensions.cs b/GFramework.Core/Extensions/ArrayPoolExtensions.cs
index ab3e7ea..6bb62f0 100644
--- a/GFramework.Core/Extensions/ArrayPoolExtensions.cs
+++ b/GFramework.Core/Extensions/ArrayPoolExtensions.cs
@@ -89,15 +89,18 @@ public static class ArrayPoolExtensions
/// 可自动释放的数组包装器
///
/// 数组元素类型
- public readonly struct ScopedArray : IDisposable
+ public ref struct ScopedArray
{
private readonly ArrayPool _pool;
private readonly bool _clearOnReturn;
+ private T[]? _array;
+#pragma warning disable CA1819
///
/// 获取租用的数组
///
- public T[] Array { get; }
+ public T[] Array => GetArray();
+#pragma warning restore CA1819
///
/// 获取数组的长度
@@ -108,7 +111,7 @@ public static class ArrayPoolExtensions
{
_pool = pool;
_clearOnReturn = clearOnReturn;
- Array = pool.Rent(minimumLength);
+ _array = pool.Rent(minimumLength);
}
///
@@ -116,7 +119,11 @@ public static class ArrayPoolExtensions
///
public void Dispose()
{
- _pool.Return(Array, _clearOnReturn);
+ if (_array is null)
+ return;
+
+ _pool.Return(_array, _clearOnReturn);
+ _array = null;
}
///
@@ -131,6 +138,24 @@ public static class ArrayPoolExtensions
/// 起始索引
/// 长度
/// 数组指定范围的 Span
- public Span AsSpan(int start, int length) => Array.AsSpan(start, length);
+ public Span AsSpan(int start, int length)
+ => Array.AsSpan(start, length);
+
+ ///
+ /// 获取数组指定索引处的引用
+ ///
+ /// 要获取引用的索引位置
+ /// 指定索引处元素的引用
+ public ref T this[int index] => ref Array[index];
+
+ ///
+ /// 获取内部数组实例
+ ///
+ /// 内部数组实例
+ /// 当对象已被丢弃时抛出
+ private T[] GetArray()
+ {
+ return _array ?? throw new ObjectDisposedException(nameof(ScopedArray));
+ }
}
-}
+}
\ No newline at end of file