0
我有一个对象,只能通过一个隐式转换运算符来访问内部字节[]。事情是这样的:演员优化和GCHandle
class Thing
{
private byte[] array;
public static implicit operator byte[](Thing thing) => thing.array;
}
我需要引脚该数组传递作为一个IntPtr:
void Foo(Thing thing)
{
byte[] array = thing; // cast to access inner byte[]
var handle = GCHandle.Alloc(array, GCHandleType.Pinned);
DoSomethingWithArray(GCHandle.ToIntPtr(handle));
handle.Free();
}
看起来这可能是可能的(在某些情况下我还没有遇到过)的编译器优化将导致错误物体被钉住的投射。是吗?
这似乎不可能,因为演员阵容是程序运行所必需的。是什么让你认为这是优化的结果? – Bas
我想我只是偏执狂。 – Tergiver
即使在返回后'DoSomethingWithArray'是否仍持有对IntPtr的引用? –