c# 结构体定义中有数组


[StructLayout(LayoutKind.Explicit,Pack=1,CharSet=CharSet.Unicode,Size=64+2)]
        public unsafe struct Repository
        {
            /// 
            /// 仓库名称
            /// 
            [FieldOffset(0)]
            [MarshalAs(UnmanagedType.LPWStr, SizeConst = 64)]
            public fixed byte Name[32];  //加上fixed就可以使用c语言语法的数组
 
            /// 
            /// 指针
            /// 
            [FieldOffset(64)]
            public ushort location;
 

第二种定义方法

public unsafe struct MemoryInfo
    {
	   public UInt32 ModuleSize;
	   public UInt32 EntryPoint;
	   public UInt64 VirtualAddress;
	   public bool Readable;
	   public bool Writeable;
	   public bool Executeable;
	   public bool Guardable;
	  //如果使用char 占用字节会翻倍  使用bute就是真实大小
	   public fixed byte ModulePath[260];
    };

获取结构体大小

System.Runtime.InteropServices.Marshal.SizeOf(typeof(MemoryInfo))

你可能感兴趣的:(c#)