当这行代码执行时抛出此异常
retobj = Marshal.PtrToStructure( buffer, anytype );
Run Code Online (Sandbox Code Playgroud)
我不知道是什么原因造成的,因为我尝试运行的应用程序在其他开发人员的计算机上运行良好。
public static object RawDeserialize(byte[] rawdatas, Type anytype)
{
int rawsize = Marshal.SizeOf(anytype);
if (rawsize > rawdatas.Length)
{
return null;
}
IntPtr buffer = Marshal.AllocHGlobal(rawsize);
object retobj = null;
try
{
Marshal.Copy(rawdatas, 0, buffer, rawsize);
retobj = Marshal.PtrToStructure(buffer, anytype);
}
finally
{
Marshal.FreeHGlobal(buffer);
}
return retobj;
}
Run Code Online (Sandbox Code Playgroud)
我已多次尝试修复 .NET Compact Framework,但似乎没有任何效果,有人知道解决此问题的方法吗?
如果您要调试程序,您会发现以下行抛出异常:
retobj = Marshal.PtrToStructure(buffer, anytype);
Run Code Online (Sandbox Code Playgroud)
主要原因是编组工具不知道如何编组您的类型。这有很多可能的原因,我知道的最常见的两个是:
struct 中的嵌套结构(anytype 类型)
[StructLayout(LayoutKind.Sequential, Pack = 1)]
嵌套数组。
[MarshalAs(UnmanagedType.ByValArray,SizeConst = 512)]
希望能帮助到你。
| 归档时间: |
|
| 查看次数: |
4467 次 |
| 最近记录: |