ModuleBuilder.DefineInitializedData(String, Byte[], FieldAttributes) Method

Definition

Defines an initialized data field in the .sdata section of the portable executable (PE) file.

C#
public System.Reflection.Emit.FieldBuilder DefineInitializedData(string name, byte[] data, System.Reflection.FieldAttributes attributes);

Parameters

name
String

The name used to refer to the data. name cannot contain embedded nulls.

data
Byte[]

The binary large object (BLOB) of data.

attributes
FieldAttributes

The attributes for the field. The default is Static.

Returns

A field to reference the data.

Exceptions

The length of name is zero.

-or-

The size of data is less than or equal to zero or greater than or equal to 0x3f0000.

name or data is null.

Examples

The following example uses the DefineInitializedData method to define an initialized data field in the .sdata section of the portable executable (PE) file.

C#
AppDomain currentDomain;
AssemblyName myAssemblyName;

// Get the current application domain for the current thread.
currentDomain = AppDomain.CurrentDomain;
myAssemblyName = new AssemblyName();
myAssemblyName.Name = "TempAssembly";

// Define a dynamic assembly in the 'currentDomain'.
myAssemblyBuilder =
   currentDomain.DefineDynamicAssembly
               (myAssemblyName, AssemblyBuilderAccess.Run);

// Define a dynamic module in "TempAssembly" assembly.
myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("TempModule");

// Define the initialized data field in the .sdata section of the PE file.
FieldBuilder myFieldBuilder =
    myModuleBuilder.DefineInitializedData("MyField",new byte[]{01,00,01},
               FieldAttributes.Static|FieldAttributes.Public);
 myModuleBuilder.CreateGlobalFunctions();

Remarks

Static is automatically included in attributes.

The data defined by this method is not created until the CreateGlobalFunctions method is called.

Note

Starting with the .NET Framework 2.0 Service Pack 1, this member no longer requires ReflectionPermission with the ReflectionPermissionFlag.ReflectionEmit flag. (See Security Issues in Reflection Emit.) To use this functionality, your application should target the .NET Framework 3.5 or later.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided), 2.1