mscorlib(4.0.0.0) API with additions
StructLayoutAttribute.cs
1 using System.Reflection;
2 using System.Security;
3 
5 {
7  [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = false)]
8  [ComVisible(true)]
9  [__DynamicallyInvokable]
10  public sealed class StructLayoutAttribute : Attribute
11  {
12  private const int DEFAULT_PACKING_SIZE = 8;
13 
14  internal LayoutKind _val;
15 
17  [__DynamicallyInvokable]
18  public int Pack;
19 
21  [__DynamicallyInvokable]
22  public int Size;
23 
25  [__DynamicallyInvokable]
26  public CharSet CharSet;
27 
30  [__DynamicallyInvokable]
31  public LayoutKind Value
32  {
33  [__DynamicallyInvokable]
34  get
35  {
36  return _val;
37  }
38  }
39 
40  [SecurityCritical]
41  internal static Attribute GetCustomAttribute(RuntimeType type)
42  {
43  if (!IsDefined(type))
44  {
45  return null;
46  }
47  int packSize = 0;
48  int classSize = 0;
49  LayoutKind layoutKind = LayoutKind.Auto;
50  switch (type.Attributes & TypeAttributes.LayoutMask)
51  {
52  case TypeAttributes.ExplicitLayout:
53  layoutKind = LayoutKind.Explicit;
54  break;
55  case TypeAttributes.NotPublic:
56  layoutKind = LayoutKind.Auto;
57  break;
58  case TypeAttributes.SequentialLayout:
59  layoutKind = LayoutKind.Sequential;
60  break;
61  }
62  CharSet charSet = CharSet.None;
63  switch (type.Attributes & TypeAttributes.StringFormatMask)
64  {
65  case TypeAttributes.NotPublic:
66  charSet = CharSet.Ansi;
67  break;
68  case TypeAttributes.AutoClass:
69  charSet = CharSet.Auto;
70  break;
71  case TypeAttributes.UnicodeClass:
72  charSet = CharSet.Unicode;
73  break;
74  }
75  type.GetRuntimeModule().MetadataImport.GetClassLayout(type.MetadataToken, out packSize, out classSize);
76  if (packSize == 0)
77  {
78  packSize = 8;
79  }
80  return new StructLayoutAttribute(layoutKind, packSize, classSize, charSet);
81  }
82 
83  internal static bool IsDefined(RuntimeType type)
84  {
85  if (type.IsInterface || type.HasElementType || type.IsGenericParameter)
86  {
87  return false;
88  }
89  return true;
90  }
91 
92  internal StructLayoutAttribute(LayoutKind layoutKind, int pack, int size, CharSet charSet)
93  {
94  _val = layoutKind;
95  Pack = pack;
96  Size = size;
97  CharSet = charSet;
98  }
99 
102  [__DynamicallyInvokable]
104  {
105  _val = layoutKind;
106  }
107 
110  public StructLayoutAttribute(short layoutKind)
111  {
112  _val = (LayoutKind)layoutKind;
113  }
114  }
115 }
int Size
Indicates the absolute size of the class or structure.
Represents the base class for custom attributes.
Definition: Attribute.cs:15
LayoutKind
Controls the layout of an object when exported to unmanaged code.
Definition: LayoutKind.cs:7
Definition: __Canon.cs:3
StructLayoutAttribute(short layoutKind)
Initalizes a new instance of the T:System.Runtime.InteropServices.StructLayoutAttribute class with th...
Lets you control the physical layout of the data fields of a class or structure in memory.
LayoutKind Value
Gets the T:System.Runtime.InteropServices.LayoutKind value that specifies how the class or structure ...
int Pack
Controls the alignment of data fields of a class or structure in memory.
CharSet
Dictates which character set marshaled strings should use.
Definition: CharSet.cs:7
TypeAttributes
Specifies type attributes.
AttributeTargets
Specifies the application elements on which it is valid to apply an attribute.
CharSet CharSet
Indicates whether string data fields within the class should be marshaled as LPWSTR or LPSTR by defau...
StructLayoutAttribute(LayoutKind layoutKind)
Initalizes a new instance of the T:System.Runtime.InteropServices.StructLayoutAttribute class with th...