嘿,我想知道是否有解决以下问题的方法,因为我似乎在任何地方都找不到,而且我现在已经没有想法了。
我正在编写一个使用 Xamarin.Forms 跨 android 和 IOS 运行的程序,以下两个代码版本都可以在 android 上运行,但是当第二个版本在 IOS 上运行时抛出 ExecutionEngineException 但第一个版本可以运行。
protected static object ReadStruct(BinaryReader reader, Type structType, ChunkHeader chunkHeader)
{
int size = Marshal.SizeOf(structType);
if (size != chunkHeader.chunkSize) // check the actual size of this chunk object with the expected size from the stream
{
throw new IOException("Invalid file format, incorrect " + chunkHeader.chunkName + " chunk size (exp.: " + size + ", read: " + chunkHeader.chunkSize + ")");
}
byte[] data = reader.ReadBytes(size);
IntPtr buffer = Marshal.AllocHGlobal(size);
Marshal.Copy(data, 0, buffer, size);
// the line that crashes follows this
object structObject = Marshal.PtrToStructure(buffer, structType);
Marshal.FreeHGlobal(buffer);
return structObject;
}
上面的代码在两个版本中保持不变。
public struct OvdBChunk
{
// stuff in here but not important
}
上面的示例有效,但对于这个和所有未来的更新,我将需要继承旧版本,因为新设备中的东西可能已经更新,所以已经添加到但将始终保留旧的东西,所以我将其更改为以下
public class OvdBChunk : OvdAChunk
{
// stuff in here but not important
}
structType 部分是顶部代码片段中正在更改的部分。
知道为什么当它是一个类而不是一个支柱时,它会抛出System.ExecutionEngineException
. 以及有关如何修复它的任何想法?