在 C# 中,您不能创建指向托管类型的指针,但使用此 API,您可以使用Unsafe.AsPointer<T>.
https://www.nuget.org/packages/System.Runtime.CompilerServices.Unsafe/
我看到使用 ILSpy 的源代码,我看到了这个:
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[System.Runtime.Versioning.NonVersionable]
public unsafe static void* AsPointer<T>(ref T value)
{
return &value;
}
同样在其他类似的 API 中:
//Unity.Collections.LowLevel.Unsafe.UnsafeUtility
public unsafe static T ReadArrayElement<T>(void* source, int index)
{
return *(T*)((byte*)source + index * sizeof(T));
}
这是如何工作的以及如何复制这种行为?
天涯尽头无女友
相关分类