diff --git a/GFramework.Core/Extensions/ArrayPoolExtensions.cs b/GFramework.Core/Extensions/ArrayPoolExtensions.cs index 64d6fd5..80a070a 100644 --- a/GFramework.Core/Extensions/ArrayPoolExtensions.cs +++ b/GFramework.Core/Extensions/ArrayPoolExtensions.cs @@ -89,7 +89,7 @@ public static class ArrayPoolExtensions /// 可自动释放的数组包装器 /// /// 数组元素类型 - public ref struct ScopedArray + public ref struct ScopedArray : IDisposable { private readonly ArrayPool _pool; private readonly bool _clearOnReturn; @@ -99,13 +99,13 @@ public static class ArrayPoolExtensions /// /// 获取租用的数组 /// - public T[] Array => _array!; + public T[] Array => GetArray(); #pragma warning restore CA1819 /// /// 获取数组的长度 /// - public int Length => _array!.Length; + public int Length => Array.Length; internal ScopedArray(ArrayPool pool, int minimumLength, bool clearOnReturn) { @@ -130,7 +130,7 @@ public static class ArrayPoolExtensions /// 获取数组的 Span 视图 /// /// 数组的 Span - public Span AsSpan() => _array!; + public Span AsSpan() => Array.AsSpan(); /// /// 获取数组指定范围的 Span 视图 @@ -139,13 +139,23 @@ public static class ArrayPoolExtensions /// 长度 /// 数组指定范围的 Span public Span AsSpan(int start, int length) - => _array!.AsSpan(start, length); + => Array.AsSpan(start, length); /// /// 获取数组指定索引处的引用 /// /// 要获取引用的索引位置 /// 指定索引处元素的引用 - public ref T this[int index] => ref _array![index]; + 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