How to get the size when a struct array in aother struct ?

S

SleepSheep

two struct in c#

[StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]
public class A
{
int i1;
byte i2;
}

[StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]
public class B
{
[MarshalAs( UnmanagedType.ByValArray, SizeConst=25 )]
A[] a;
}

then i call Marshal.SizeOf(typeof(B)) will exception and talk me
{"Type B can not be marshaled as an unmanaged structure; no meaningful
size or offset can be computed." }

How to get the size when a struct array in aother struct ?
Thanks
 
M

Mattias Sjögren

[StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]
public class B
{
[MarshalAs( UnmanagedType.ByValArray, SizeConst=25 )]
A[] a;
}

ByVal arrays of structs isn't supported by the marshaler, you will not
be able to use such a struct in interop calls. So getting it's size if
probably not meaningful either. But you can still do

25 * Marshal.SizeOf(typeof(A))



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top