C# Marshal.Sizeof() when using custom marshaler

孤街浪徒 提交于 2020-01-04 09:54:18

问题


Is it possible to use Marshal.SizeOf() on a structure which is using a custom marshaler?

For example:

struct Abcde { 
  public int test1;
  [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(MyCustomMarshaler)]
  public string customString1;
}

If I call:

var size = Marshal.SizeOf(typeof(Abcde));

an exception is thrown which says that no meaningful size or offset can be computed. I noticed that ICustomMarshaler has a method called GetNativeDataSize() but the exception is thrown regardless what I return there.


回答1:


Marshal.SizeOf cannot be used with types that contain custom marshaled members.



来源:https://stackoverflow.com/questions/23239320/c-sharp-marshal-sizeof-when-using-custom-marshaler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!