Skip to content

Commit d668d93

Browse files
author
dahall
committed
Fixed Array.MarshalToPtr bug
1 parent 04b1424 commit d668d93

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

Core/Extensions/InteropExtensions.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,13 @@ public static IntPtr MarshalToPtr(this Array items, Func<int, IntPtr> memAlloc,
362362
throw new ArgumentException("Only single dimension arrays are supported.", nameof(items));
363363
if (items.GetType().GetElementType() == typeof(string))
364364
return items.Cast<string?>().MarshalToPtr(StringListPackMethod.Concatenated, memAlloc, out bytesAllocated, CharSet.Auto, prefixBytes, memLock, memUnlock);
365-
return (IntPtr)typeof(IEnumerable<>).MakeGenericType(items.GetType().GetElementType()!).GetMethod(nameof(MarshalToPtr), new[] { typeof(IEnumerable<>), typeof(Func<int, IntPtr>), typeof(int).MakeByRefType(), typeof(SizeT), typeof(Func<IntPtr, IntPtr>), typeof(Func<IntPtr, bool>) })!
366-
.MakeGenericMethod(items.GetType().GetElementType()!).Invoke(null, new object?[] { items, memAlloc, bytesAllocated = 0, prefixBytes, memLock, memUnlock }!)!;
365+
bytesAllocated = 0;
366+
#pragma warning disable IL2060
367+
return (IntPtr)typeof(InteropExtensions).GetMethods()
368+
.First(static m => m.Name == "MarshalToPtr" && m.GetParameters()[0].ParameterType.Name == "T[]")
369+
.MakeGenericMethod(items.GetType().GetElementType()!)
370+
.Invoke(null, new object?[] { items, memAlloc, bytesAllocated, prefixBytes, memLock, memUnlock }!)!;
371+
#pragma warning restore IL2060
367372
}
368373

369374
/// <summary>

0 commit comments

Comments
 (0)