Hello,
(code below).
The size of MyCollection is 20, but when I change class MyObject to
struct MyObject, the size is 50 (which is what I expect). I understand
this is because structs are value type, where as when I have it as a
class, the array will be references.
However, it must have something to do with the array, as if I do:
public MyObject myObject1;
public MyObject myObject2;
public MyObject myObjectn;
I get the correct size whether it's a struct or a class.
Is there a way to keep class MyObject and have Marshal.SizeOf() return
50 (i.e any other attributes I'm not aware of) with an array of my own
types? If not, could you explain why?
Thanks,
Paul
// size wants to be 10 * 5 = 50
// reported size is 4 * 5 = 20 when using class
[StructLayout( LayoutKind.Sequential )]
class MyObject
{
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=10 )]
public string myName;
}
[StructLayout( LayoutKind.Sequential )]
class MyCollection
{
[MarshalAs( UnmanagedType.ByValArray, SizeConst=5 )]
public MyObject[] myObjects;
}
....
int n = Marshal.SizeOf( typeof( MyCollection ) );