|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.|
Defining a Field with Reflection Emit
A field is defined as a type member using the TypeBuilder.DefineField method. The DefineField method requires the caller to specify the field name, the field type, and the field attributes (using the FieldAttributes enumeration). DefineField returns a FieldBuilder.
FieldBuilder.SetOffset defines field layout. FieldBuilder.SetMarshal defines how a field is marshaled. FieldBuilder.SetConstant defines the default value of a field.
A field can also reference data in the .sdata section of the portable executable (PE) file. Fields representing initialized data are defined using ModuleBuilder.DefineInitializedData. Fields representing uninitialized data are defined using ModuleBuilder.DefineUninitializedData.
Static fields are specified using the FieldAttributes.Static attribute.
Fields that can be initialized but not subsequently written to are specified using the FieldAttributes.InitOnly attribute.
Fields that are compile-time constants are specified using the FieldAttributes.Literal attribute.
There are several attributes that determine field visibility. See the description of the FieldAttributes enumeration.