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

定义

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

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

参数

name
String

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

data
Byte[]

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

attributes
FieldAttributes

字段的属性。 默认值为 Static

返回

用于引用数据的字段。

例外

name 的长度为零。

- 或 -

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

namedatanull

示例

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

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

注解

Static 会自动包含在 中 attributes

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

备注

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

适用于

产品 版本
.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