ModuleBuilder.DefineInitializedData Method

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

Namespace: System.Reflection.Emit
Assembly: mscorlib (in mscorlib.dll)

public FieldBuilder DefineInitializedData (
	string name,
	byte[] data,
	FieldAttributes attributes
)
public FieldBuilder DefineInitializedData (
	String name, 
	byte[] data, 
	FieldAttributes attributes
)
public function DefineInitializedData (
	name : String, 
	data : byte[], 
	attributes : FieldAttributes
) : FieldBuilder
Not applicable.

Parameters

name

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

data

The blob of data.

attributes

The attributes for the field. The default is Static.

Return Value

A field to reference the data.

Exception typeCondition

ArgumentException

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.

ArgumentNullException

name or data is a null reference (Nothing in Visual Basic).

InvalidOperationException

CreateGlobalFunctions has been previously called.

Static is automatically included in attributes.

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

NoteNote:

Starting with the , 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 . For more information, see The .NET Framework 3.5 Architecture.

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

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();

AppDomain currentDomain;
AssemblyName myAssemblyName;

// Get the current application domain for the current thread.
currentDomain = AppDomain.get_CurrentDomain();

myAssemblyName = new AssemblyName();
myAssemblyName.set_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 ubyte[] { 1, 0, 1 }, 
    FieldAttributes.Static | FieldAttributes.Public);
myModuleBuilder.CreateGlobalFunctions();

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show: