ModuleBuilder.DefineInitializedData(String, Byte[], FieldAttributes) 方法

定义

在可移植可执行 (PE) 文件的 .sdata 部分定义已初始化的数据字段。

public:
 System::Reflection::Emit::FieldBuilder ^ DefineInitializedData(System::String ^ name, cli::array <System::Byte> ^ data, System::Reflection::FieldAttributes attributes);
public System.Reflection.Emit.FieldBuilder DefineInitializedData (string name, byte[] data, System.Reflection.FieldAttributes attributes);
member this.DefineInitializedData : string * byte[] * System.Reflection.FieldAttributes -> System.Reflection.Emit.FieldBuilder
Public Function DefineInitializedData (name As String, data As Byte(), attributes As FieldAttributes) As FieldBuilder

参数

name
String

用于引用数据的名称。 name 不能包含嵌入的 null。

data
Byte[]

数据的二进制大对象 (BLOB)。

attributes
FieldAttributes

字段的属性。 默认值为 Static

返回

用于引用数据的字段。

例外

name 的长度为零。

- 或 -

data 的大小小于等于零,或者大于等于 0x3f0000。

namedatanull

示例

以下示例使用 DefineInitializedData 方法在可移植可执行文件 (PE) 文件的 节中 .sdata 定义初始化的数据字段。

AppDomain^ currentDomain;
AssemblyName^ myAssemblyName;

// Get the current application domain for the current thread.
currentDomain = AppDomain::CurrentDomain;
myAssemblyName = gcnew 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.
array<Byte>^ temp0 = {01,00,01};
FieldBuilder^ myFieldBuilder =
   myModuleBuilder->DefineInitializedData( "MyField", temp0,
      (FieldAttributes)(FieldAttributes::Static | FieldAttributes::Public) );
myModuleBuilder->CreateGlobalFunctions();
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();
Dim currentDomain As AppDomain
Dim myAssemblyName As AssemblyName
' 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.
Dim myFieldBuilder As FieldBuilder = _
      myModuleBuilder.DefineInitializedData("MyField", New Byte() {1, 0, 1}, _
      FieldAttributes.Static Or FieldAttributes.Public)
myModuleBuilder.CreateGlobalFunctions()

注解

Static 会自动包含在 中 attributes

在调用 方法之前, CreateGlobalFunctions 不会创建此方法定义的数据。

注意

从 .NET Framework 2.0 Service Pack 1 开始,此成员不再需要ReflectionPermission带有 ReflectionPermissionFlag.ReflectionEmit 标志。 (请参阅 Reflection Emit.) 中的安全问题若要使用此功能,应用程序应面向 .NET Framework 3.5 或更高版本。

适用于