C# array within a struct

前端 未结 5 1042
余生分开走
余生分开走 2020-12-01 10:59

Want to do this: (EDIT: bad sample code, ignore and skip below)

struct RECORD {
    char[] name = new char[16];
    int dt1;
}
struct BLOCK {
    char[] ver         


        
5条回答
  •  甜味超标
    2020-12-01 11:25

    Unmanaged structures can contain embedded arrays. By default, these embedded array fields are marshaled as a SAFEARRAY. In the following example, s1 is an embedded array that is allocated directly within the structure itself.

    Unmanaged representation
    struct MyStruct {
        short s1[128];
    }
    

    Arrays can be marshaled as UnmanagedType.ByValArray, which requires you to set the MarshalAsAttribute.SizeConst field. The size can be set only as a constant. The following code shows the corresponding managed definition of MyStruct. C#VB

    [StructLayout(LayoutKind.Sequential)]
    public struct MyStruct {
       [MarshalAs(UnmanagedType.ByValArray, SizeConst=128)] public short[] s1;
    }
    

提交回复
热议问题