diff --git a/Il2CppInterop.Runtime/Il2CppInterop.Runtime.csproj b/Il2CppInterop.Runtime/Il2CppInterop.Runtime.csproj
index 0766a12b..0815e6b0 100644
--- a/Il2CppInterop.Runtime/Il2CppInterop.Runtime.csproj
+++ b/Il2CppInterop.Runtime/Il2CppInterop.Runtime.csproj
@@ -10,6 +10,8 @@
Debug;Release
AnyCPU
false
+ Latest
+ enable
diff --git a/Il2CppInterop.Runtime/InteropTypes/Arrays/Il2CppStructArray.cs b/Il2CppInterop.Runtime/InteropTypes/Arrays/Il2CppStructArray.cs
index 7553eaee..d1e5b07f 100644
--- a/Il2CppInterop.Runtime/InteropTypes/Arrays/Il2CppStructArray.cs
+++ b/Il2CppInterop.Runtime/InteropTypes/Arrays/Il2CppStructArray.cs
@@ -1,4 +1,5 @@
using System;
+using System.Diagnostics.CodeAnalysis;
namespace Il2CppInterop.Runtime.InteropTypes.Arrays;
@@ -9,6 +10,11 @@ static Il2CppStructArray()
StaticCtorBody(typeof(Il2CppStructArray));
}
+ ///
+ /// The pointer to the first element in the array.
+ ///
+ private IntPtr ArrayStartPointer => IntPtr.Add(Pointer, 4 * IntPtr.Size);
+
public Il2CppStructArray(IntPtr nativeObject) : base(nativeObject)
{
}
@@ -19,37 +25,38 @@ public Il2CppStructArray(long size) : base(AllocateArray(size))
public Il2CppStructArray(T[] arr) : base(AllocateArray(arr.Length))
{
- for (var i = 0; i < arr.Length; i++)
- this[i] = arr[i];
+ arr.CopyTo(this);
}
- public override unsafe T this[int index]
+ public override T this[int index]
{
- get
- {
- if (index < 0 || index >= Length)
- throw new ArgumentOutOfRangeException(nameof(index),
- "Array index may not be negative or above length of the array");
- var arrayStartPointer = IntPtr.Add(Pointer, 4 * IntPtr.Size);
- return ((T*)arrayStartPointer.ToPointer())[index];
- }
- set
- {
- if (index < 0 || index >= Length)
- throw new ArgumentOutOfRangeException(nameof(index),
- "Array index may not be negative or above length of the array");
- var arrayStartPointer = IntPtr.Add(Pointer, 4 * IntPtr.Size);
- ((T*)arrayStartPointer.ToPointer())[index] = value;
- }
+ get => AsSpan()[index];
+ set => AsSpan()[index] = value;
}
- public static implicit operator Il2CppStructArray(T[] arr)
+ public unsafe Span AsSpan()
+ {
+ return new Span(ArrayStartPointer.ToPointer(), Length);
+ }
+
+ [return: NotNullIfNotNull(nameof(arr))]
+ public static implicit operator Il2CppStructArray?(T[]? arr)
{
if (arr == null) return null;
return new Il2CppStructArray(arr);
}
+ public static implicit operator Span(Il2CppStructArray? il2CppArray)
+ {
+ return il2CppArray is not null ? il2CppArray.AsSpan() : default;
+ }
+
+ public static implicit operator ReadOnlySpan(Il2CppStructArray? il2CppArray)
+ {
+ return il2CppArray is not null ? il2CppArray.AsSpan() : default;
+ }
+
private static IntPtr AllocateArray(long size)
{
if (size < 0)