TypeBuilder.DefineUninitializedData(String, Int32, FieldAttributes) 方法

定義

在可攜式執行檔 (PE) 的 .sdata 區段中定義未初始化的資料欄位。

public:
 System::Reflection::Emit::FieldBuilder ^ DefineUninitializedData(System::String ^ name, int size, System::Reflection::FieldAttributes attributes);
public System.Reflection.Emit.FieldBuilder DefineUninitializedData (string name, int size, System.Reflection.FieldAttributes attributes);
member this.DefineUninitializedData : string * int * System.Reflection.FieldAttributes -> System.Reflection.Emit.FieldBuilder
Public Function DefineUninitializedData (name As String, size As Integer, attributes As FieldAttributes) As FieldBuilder

參數

name
String

用來參考資料的名稱。 name 不能包含內嵌的 null。

size
Int32

資料欄位的大小。

attributes
FieldAttributes

欄位的屬性。

傳回

要參考資料的欄位。

例外狀況

name 的長度為零。

-或-

size 小於或等於零,或者大於或等於 0x003f0000。

namenull

先前使用 CreateType() 建立的類型。

範例

下列程式碼範例示範如何使用 DefineUninitializedData 在動態類型中建立未初始化的資料欄位:

using namespace System;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Runtime::InteropServices;
using namespace System::Security::Permissions;

public ref class Example
{
public:
   [SecurityPermission(SecurityAction::Demand, Flags=SecurityPermissionFlag::UnmanagedCode)]
   static void Main()
   {
      Type^ myHelloWorldType = CreateCallee( Thread::GetDomain() );
      Object^ myHelloWorldInstance = Activator::CreateInstance( myHelloWorldType );
      FieldInfo^ myGreetingFieldInfo = myHelloWorldType->GetField( "MyGreeting" );
      Object^ oval = Activator::CreateInstance( myGreetingFieldInfo->FieldType );
      IntPtr myIntPtr = Marshal::AllocHGlobal( 4 );
      Random^ rand = gcnew Random;
      int iTempSeed = rand->Next();
      array<Byte>^bINITBYTE = GetRandBytes( iTempSeed, 4 );
      IntPtr intptrTemp = myIntPtr;
      for ( int j = 0; j < 4; j++ )
      {
         Marshal::WriteByte( myIntPtr, bINITBYTE[ j ] );
         myIntPtr = (IntPtr)((int)myIntPtr + 1);

      }
      myIntPtr = intptrTemp;
      Object^ oValNew = Marshal::PtrToStructure( myIntPtr, myGreetingFieldInfo->FieldType );
      Marshal::FreeHGlobal( myIntPtr );
      myIntPtr = Marshal::AllocHGlobal( 4 );
      Object^ myObj = myGreetingFieldInfo->GetValue( myHelloWorldInstance );
      Marshal::StructureToPtr( myObj, myIntPtr, true );
      intptrTemp = myIntPtr;
      Console::WriteLine( "The value of 'MyGreeting' field : " );
      for ( int j = 0; j < 4; j++ )
      {
         Marshal::WriteByte( myIntPtr, bINITBYTE[ j ] );
         Console::WriteLine( bINITBYTE[ j ] );
         myIntPtr = (IntPtr)((int)myIntPtr + 1);

      }
   }


private:
   static array<Byte>^ GetRandBytes( int iRandSeed, int iSize )
   {
      array<Byte>^barr = gcnew array<Byte>(iSize);
      Random^ randTemp = gcnew Random( iRandSeed );
      randTemp->NextBytes( barr );
      return barr;
   }


   // Create the callee transient dynamic assembly.
   static Type^ CreateCallee( AppDomain^ myDomain )
   {
      
      // Create a simple name for the callee assembly.
      AssemblyName^ myAssemblyName = gcnew AssemblyName;
      myAssemblyName->Name = "EmittedClass";
      
      // Create the callee dynamic assembly.
      AssemblyBuilder^ myAssembly = myDomain->DefineDynamicAssembly( myAssemblyName, AssemblyBuilderAccess::Run );
      
      // Create a dynamic module in the callee assembly.
      ModuleBuilder^ myModule = myAssembly->DefineDynamicModule( "EmittedModule" );
      
      // Define a public class named "MyHelloWorld"
      TypeBuilder^ myHelloWorldType = myModule->DefineType( "MyHelloWorld", TypeAttributes::Public );
      
      // Define a 'MyGreeting' field and initialize it.
      FieldBuilder^ myFieldBuilder = myHelloWorldType->DefineUninitializedData( "MyGreeting", 4, FieldAttributes::Public );
      
      // Create the 'MyHelloWorld' class.
      return (myHelloWorldType->CreateType());
   }

};

int main()
{
   Example::Main();
}
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
using System.Security.Permissions;

public sealed class Example
{
   public static void Main()
   {
      Type myHelloWorldType = CreateCallee(Thread.GetDomain());
      object myHelloWorldInstance =
      Activator.CreateInstance(myHelloWorldType);
      FieldInfo myGreetingFieldInfo =
      myHelloWorldType.GetField("MyGreeting");
      object oval = Activator.CreateInstance(myGreetingFieldInfo.FieldType);
      IntPtr myIntPtr = Marshal.AllocHGlobal(4);
      Random rand = new Random();
      int iTempSeed = rand.Next();
      byte[] bINITBYTE = GetRandBytes( iTempSeed, 4);
      IntPtr intptrTemp = myIntPtr;
      for ( int j = 0; j < 4; j++ )
      {
         Marshal.WriteByte( myIntPtr, bINITBYTE[j]);
         myIntPtr = (IntPtr)((int)myIntPtr + 1);
      }
      myIntPtr = intptrTemp;
      Object oValNew = Marshal.PtrToStructure( myIntPtr, myGreetingFieldInfo.FieldType);
      Marshal.FreeHGlobal( myIntPtr );

      myIntPtr = Marshal.AllocHGlobal(4);
      object myObj = myGreetingFieldInfo.GetValue(myHelloWorldInstance);
      Marshal.StructureToPtr(myObj, myIntPtr, true);
      intptrTemp = myIntPtr;
      Console.WriteLine("The value of 'MyGreeting' field : ");
      for ( int j = 0; j < 4; j++ )
      {
         Marshal.WriteByte( myIntPtr, bINITBYTE[j]);
         Console.WriteLine(bINITBYTE[j]);
         myIntPtr = (IntPtr)((int)myIntPtr + 1);
      }
   }

   private static byte[] GetRandBytes( int iRandSeed, int iSize )
   {
      byte[] barr = new byte[iSize];
      Random randTemp = new Random( iRandSeed );
      randTemp.NextBytes( barr );
      return barr;
   }

   // Create the callee transient dynamic assembly.
   private static Type CreateCallee(AppDomain myDomain)
   {
      // Create a simple name for the callee assembly.
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "EmittedClass";

      // Create the callee dynamic assembly.
      AssemblyBuilder myAssembly =
         myDomain.DefineDynamicAssembly(myAssemblyName,AssemblyBuilderAccess.Run);

      // Create a dynamic module in the callee assembly.
      ModuleBuilder myModule =
         myAssembly.DefineDynamicModule("EmittedModule");

      // Define a public class named "MyHelloWorld"
      TypeBuilder myHelloWorldType =
         myModule.DefineType("MyHelloWorld", TypeAttributes.Public);

      // Define a 'MyGreeting' field and initialize it.
      FieldBuilder myFieldBuilder =
         myHelloWorldType.DefineUninitializedData("MyGreeting",4,FieldAttributes.Public);

      // Create the 'MyHelloWorld' class.
      return(myHelloWorldType.CreateType());
   }

   private Example() {}
}
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Runtime.InteropServices


Module Example

   Sub Main()
      Dim myHelloWorldType As Type = CreateCallee(Thread.GetDomain())
      Dim myHelloWorldInstance As Object = Activator.CreateInstance(myHelloWorldType)
      Dim myGreetingFieldInfo As FieldInfo = myHelloWorldType.GetField("MyGreeting")
      Dim oval As Object = Activator.CreateInstance(myGreetingFieldInfo.FieldType)
      Dim myIntPtr As IntPtr = Marshal.AllocHGlobal(4)
      Dim rand As New Random()
      Dim iTempSeed As Integer = rand.Next()
      Dim bINITBYTE As Byte() = GetRandBytes(iTempSeed, 4)
      Dim intptrTemp As IntPtr = myIntPtr
      Dim j As Integer
      For j = 0 To 3
         Marshal.WriteByte(myIntPtr, bINITBYTE(j))
         myIntPtr = intptr.op_Explicit(myIntPtr.ToInt32 + 1)
      Next j
      myIntPtr = intptrTemp
      Dim oValNew As [Object] = Marshal.PtrToStructure(myIntPtr, myGreetingFieldInfo.FieldType)
      Marshal.FreeHGlobal(myIntPtr)

      myIntPtr = Marshal.AllocHGlobal(4)
      Dim myObj As Object = myGreetingFieldInfo.GetValue(myHelloWorldInstance)
      Marshal.StructureToPtr(myObj, myIntPtr, True)
      intptrTemp = myIntPtr
      Console.WriteLine("The value of 'MyGreeting' field : ")

      For j = 0 To 3
         Marshal.WriteByte(myIntPtr, bINITBYTE(j))
         Console.WriteLine(bINITBYTE(j))
         myIntPtr = intptr.op_Explicit(myIntPtr.ToInt32 + 1)
      Next j
   End Sub


   Private Function GetRandBytes(ByVal iRandSeed As Integer, ByVal iSize As Integer) As Byte()
      Dim barr(iSize) As Byte
      Dim randTemp As New Random(iRandSeed)
      randTemp.NextBytes(barr)
      Return barr
   End Function 'GetRandBytes


   ' Create the callee transient dynamic assembly.
   Private Function CreateCallee(ByVal myDomain As AppDomain) As Type
      ' Create a simple name for the callee assembly.
      Dim myAssemblyName As New AssemblyName()
      myAssemblyName.Name = "EmittedClass"

      ' Create the callee dynamic assembly.
      Dim myAssembly As AssemblyBuilder = myDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run)


      ' Create a dynamic module in the callee assembly.
      Dim myModule As ModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule")

      ' Define a public class named "MyHelloWorld"
      Dim myHelloWorldType As TypeBuilder = myModule.DefineType("MyHelloWorld", TypeAttributes.Public)

      ' Define a 'MyGreeting' field and initialize it.
      Dim myFieldBuilder As FieldBuilder = myHelloWorldType.DefineUninitializedData("MyGreeting", 4, FieldAttributes.Public)

      ' Create the 'MyHelloWorld' class.
      Return myHelloWorldType.CreateType()
   End Function 'CreateCallee

End Module

備註

您使用這個方法建立的欄位將會是 static ,即使您未包含在 FieldAttributes.Static 參數中 attributes 也一樣。

適用於