使用自定义封送拆收器时的 C# Marshal.Sizeof()

Har*_*y13 3 c# pinvoke marshalling

是否可以在使用自定义封送拆收器的结构上使用Marshal.SizeOf()

例如:

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

如果我打电话:

var size = Marshal.SizeOf(typeof(Abcde));
Run Code Online (Sandbox Code Playgroud)

抛出一个异常,表示无法计算有意义的大小或偏移量。我注意到ICustomMarshaler有一个名为GetNativeDataSize()的方法,但是无论我在那里返回什么都会抛出异常。

Dav*_*nan 5

Marshal.SizeOf 不能与包含自定义封送成员的类型一起使用。