ModuleBuilder.DefineInitializedData 方法

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

**命名空间:**System.Reflection.Emit
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Function DefineInitializedData ( _
    name As String, _
    data As Byte(), _
    attributes As FieldAttributes _
) As FieldBuilder
用法
Dim instance As ModuleBuilder
Dim name As String
Dim data As Byte()
Dim attributes As FieldAttributes
Dim returnValue As FieldBuilder

returnValue = instance.DefineInitializedData(name, data, attributes)
public FieldBuilder DefineInitializedData (
    string name,
    byte[] data,
    FieldAttributes attributes
)
public:
FieldBuilder^ DefineInitializedData (
    String^ name, 
    array<unsigned char>^ data, 
    FieldAttributes attributes
)
public FieldBuilder DefineInitializedData (
    String name, 
    byte[] data, 
    FieldAttributes attributes
)
public function DefineInitializedData (
    name : String, 
    data : byte[], 
    attributes : FieldAttributes
) : FieldBuilder

参数

  • name
    用于引用数据的名称。name 不能包含嵌入的空值。
  • data
    数据的 Blob。
  • attributes
    该字段的属性。默认为 Static

返回值

引用这些数据的字段。

异常

异常类型 条件

ArgumentException

name 的长度为零。

- 或 -

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

ArgumentNullException

name 或 data 为 空引用(在 Visual Basic 中为 Nothing)。

InvalidOperationException

以前调用过 CreateGlobalFunctions

备注

Static 自动包括在 attributes 中。

此方法定义的数据在调用 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()
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::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.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();

.NET Framework 安全性

平台

Windows 98、Windows 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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

ModuleBuilder 类
ModuleBuilder 成员
System.Reflection.Emit 命名空间