Skip to content

Latest commit

 

History

History
21 lines (11 loc) · 2.02 KB

ii.16.5.1-data-known-at-run-time.md

File metadata and controls

21 lines (11 loc) · 2.02 KB

II.16.5.1 Data known at run time

When the correct value cannot be determined until type layout is computed, the user shall supply code as part of a type initializer to initialize the static data. The guarantees about type initialization are covered in §II.10.5.3.1. As will be explained below, global statics are modeled in the CLI as though they belonged to a type, so the same guarantees apply to both global and type statics.

Because the layout of managed types need not occur until a type is first referenced, it is not possible to statically initialize managed types by simply laying out the data in the PE file. Instead, there is a type initialization process that proceeds in the following steps:

  1. All static variables are zeroed.

  2. The user-supplied type initialization procedure, if any, is invoked as described in §II.10.5.3.

Within a type initialization procedure there are several techniques:

  • Generate explicit code that stores constants into the appropriate fields of the static variables. For small data structures this can be efficient, but it requires that the initializer be converted to native code, which can prove to be both a code space and an execution time problem.

  • Box value types. When the static variable is simply a boxed version of a primitive numeric type or a value type with explicit layout, introduce an additional static variable with known RVA that holds the unboxed instance and then simply use the box instruction to create the boxed copy.

  • Create a managed array from a static native array of data. This can be done by marshaling the native array to a managed array. The specific marshaler to be used depends on the native array. e.g., it can be a safearray.

  • Default initialize a managed array of a value type. The Base Class Library provides a method that zeroes the storage for every element of an array of unboxed value types (System.Runtime.CompilerServices.InitializeArray)

End informative text.