当前位置: 首页>>代码示例>>C#>>正文


C# StructLayoutAttribute.Pack字段代码示例

本文整理汇总了C#中System.Runtime.InteropServices.StructLayoutAttribute.Pack字段的典型用法代码示例。如果您正苦于以下问题:C# StructLayoutAttribute.Pack字段的具体用法?C# StructLayoutAttribute.Pack怎么用?C# StructLayoutAttribute.Pack使用的例子?那么恭喜您, 这里精选的字段代码示例或许可以为您提供帮助。您也可以进一步了解该字段所在System.Runtime.InteropServices.StructLayoutAttribute的用法示例。


在下文中一共展示了StructLayoutAttribute.Pack字段的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1:

//引入命名空间
using System;

struct ExampleStruct
{
   public byte b1;
   public byte b2;
   public int i3;
}
开发者ID:.NET开发者,项目名称:System.Runtime.InteropServices,代码行数:9,代码来源:StructLayoutAttribute.Pack

示例2: Main

//引入命名空间
using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, Pack=0)]
struct ExampleStruct
{
   public byte b1;
   public byte b2;
   public int i3;
}

public class Example
{
   public unsafe static void Main()
   {

      ExampleStruct ex = new ExampleStruct();
      byte* addr = (byte*) &ex;
      Console.WriteLine("Size:      {0}", sizeof(ExampleStruct));
      Console.WriteLine("b1 Offset: {0}", &ex.b1 - addr);
      Console.WriteLine("b2 Offset: {0}", &ex.b2 - addr);
      Console.WriteLine("i3 Offset: {0}", (byte*) &ex.i3 - addr);
   }
}
开发者ID:.NET开发者,项目名称:System.Runtime.InteropServices,代码行数:25,代码来源:StructLayoutAttribute.Pack

输出:

Size:      8
b1 Offset: 0
b2 Offset: 1
i3 Offset: 4

示例3: Main

//引入命名空间
using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, Pack=2)]
struct ExampleStruct
{
   public byte b1;
   public byte b2;
   public int i3;
}

public class Example
{
   public unsafe static void Main()
   {

      ExampleStruct ex = new ExampleStruct();
      byte* addr = (byte*) &ex;
      Console.WriteLine("Size:      {0}", sizeof(ExampleStruct));
      Console.WriteLine("b1 Offset: {0}", &ex.b1 - addr);
      Console.WriteLine("b2 Offset: {0}", &ex.b2 - addr);
      Console.WriteLine("i3 Offset: {0}", (byte*) &ex.i3 - addr);
   }
}
开发者ID:.NET开发者,项目名称:System.Runtime.InteropServices,代码行数:25,代码来源:StructLayoutAttribute.Pack

输出:

Size:      6
b1 Offset: 0
b2 Offset: 1
i3 Offset: 2

示例4: Main

//引入命名空间
using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, Pack=4)]
struct ExampleStruct
{
   public byte b1;
   public byte b2;
   public int i3;
}

public class Example
{
   public unsafe static void Main()
   {

      ExampleStruct ex = new ExampleStruct();
      byte* addr = (byte*) &ex;
      Console.WriteLine("Size:      {0}", sizeof(ExampleStruct));
      Console.WriteLine("b1 Offset: {0}", &ex.b1 - addr);
      Console.WriteLine("b2 Offset: {0}", &ex.b2 - addr);
      Console.WriteLine("i3 Offset: {0}", (byte*) &ex.i3 - addr);
   }
}
开发者ID:.NET开发者,项目名称:System.Runtime.InteropServices,代码行数:25,代码来源:StructLayoutAttribute.Pack

输出:

Size:      8
b1 Offset: 0
b2 Offset: 1
i3 Offset: 4

示例5: Main

//引入命名空间
using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, Pack=8)]
struct ExampleStruct
{
   public byte b1;
   public byte b2;
   public int i3;
}

public class Example
{
   public unsafe static void Main()
   {

      ExampleStruct ex = new ExampleStruct();
      byte* addr = (byte*) &ex;
      Console.WriteLine("Size:      {0}", sizeof(ExampleStruct));
      Console.WriteLine("b1 Offset: {0}", &ex.b1 - addr);
      Console.WriteLine("b2 Offset: {0}", &ex.b2 - addr);
      Console.WriteLine("i3 Offset: {0}", (byte*) &ex.i3 - addr);
   }
}
开发者ID:.NET开发者,项目名称:System.Runtime.InteropServices,代码行数:25,代码来源:StructLayoutAttribute.Pack

输出:

Size:      8
b1 Offset: 0
b2 Offset: 1
i3 Offset: 4

示例6: Main

//引入命名空间
using System;
using System.Runtime.InteropServices;

unsafe struct ExampleStruct2
{

   public byte b1;
   public byte b2;
   public int i3;
   public fixed byte a4[1];
   public decimal d5;
}

public class Example
{
   public unsafe static void Main()
   {

      ExampleStruct2 ex = new ExampleStruct2();
      byte* addr = (byte*) &ex;
      Console.WriteLine("Size:      {0}", sizeof(ExampleStruct2));
      Console.WriteLine("b1 Offset: {0}", &ex.b1 - addr);
      Console.WriteLine("b2 Offset: {0}", &ex.b2 - addr);
      Console.WriteLine("i3 Offset: {0}", (byte*) &ex.i3 - addr);
      Console.WriteLine("a4 Offset: {0}", ex.a4 - addr);
      Console.WriteLine("d5 Offset: {0}", (byte*) &ex.d5 - addr);
   }
}
开发者ID:.NET开发者,项目名称:System.Runtime.InteropServices,代码行数:29,代码来源:StructLayoutAttribute.Pack

输出:

Size:      28
b1 Offset: 0
b2 Offset: 1
i3 Offset: 4
a4 Offset: 8
d5 Offset: 12

示例7: Main

//引入命名空间
using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, Pack = 2)]
unsafe struct ExampleStruct2
{

   public byte b1;
   public byte b2;
   public int i3;
   public fixed byte a4[1];
   public decimal d5;
}

public class Example
{
   public unsafe static void Main()
   {

      ExampleStruct2 ex = new ExampleStruct2();
      byte* addr = (byte*) &ex;
      Console.WriteLine("Size:      {0}", sizeof(ExampleStruct2));
      Console.WriteLine("b1 Offset: {0}", &ex.b1 - addr);
      Console.WriteLine("b2 Offset: {0}", &ex.b2 - addr);
      Console.WriteLine("i3 Offset: {0}", (byte*) &ex.i3 - addr);
      Console.WriteLine("a4 Offset: {0}", ex.a4 - addr);
      Console.WriteLine("d5 Offset: {0}", (byte*) &ex.d5 - addr);
   }
}
开发者ID:.NET开发者,项目名称:System.Runtime.InteropServices,代码行数:30,代码来源:StructLayoutAttribute.Pack

输出:

Size:      24
b1 Offset: 0
b2 Offset: 1
i3 Offset: 2
a4 Offset: 6
d5 Offset: 8

示例8: Main

//引入命名空间
using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, Pack = 8)]
unsafe struct ExampleStruct2
{

   public byte b1;
   public byte b2;
   public int i3;
   public fixed byte a4[1];
   public decimal d5;
}

public class Example
{
   public unsafe static void Main()
   {

      ExampleStruct2 ex = new ExampleStruct2();
      byte* addr = (byte*) &ex;
      Console.WriteLine("Size:      {0}", sizeof(ExampleStruct2));
      Console.WriteLine("b1 Offset: {0}", &ex.b1 - addr);
      Console.WriteLine("b2 Offset: {0}", &ex.b2 - addr);
      Console.WriteLine("i3 Offset: {0}", (byte*) &ex.i3 - addr);
      Console.WriteLine("a4 Offset: {0}", ex.a4 - addr);
      Console.WriteLine("d5 Offset: {0}", (byte*) &ex.d5 - addr);
   }
}
开发者ID:.NET开发者,项目名称:System.Runtime.InteropServices,代码行数:30,代码来源:StructLayoutAttribute.Pack

输出:

Size:      28
b1 Offset: 0
b2 Offset: 1
i3 Offset: 4
a4 Offset: 8
d5 Offset: 12


注:本文中的System.Runtime.InteropServices.StructLayoutAttribute.Pack字段示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。