This documentation is archived and is not being maintained.

TypeBuilder.DefineUninitializedData Method

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

[Visual Basic]
Public Function DefineUninitializedData( _
   ByVal name As String, _
   ByVal size As Integer, _
   ByVal attributes As FieldAttributes _
) As FieldBuilder
[C#]
public FieldBuilder DefineUninitializedData(
 string name,
 int size,
 FieldAttributes attributes
);
[C++]
public: FieldBuilder* DefineUninitializedData(
 String* name,
 int size,
 FieldAttributes attributes
);
[JScript]
public function DefineUninitializedData(
   name : String,
 size : int,
 attributes : FieldAttributes
) : FieldBuilder;

Parameters

name
The name used to refer to the data. name cannot contain embedded nulls.
size
The size of the data field.
attributes
The attributes for the field.

Return Value

A field to reference the data.

Exceptions

Exception Type Condition
ArgumentException Length of name is zero.

-or-

size is less than or equal to zero or greater than or equal to 0x003f0000.

ArgumentNullException name is a null reference (Nothing in Visual Basic).
InvalidOperationException The type was previously created using CreateType.

Remarks

The field that you create with this method will be static (Shared in Visual Basic), even if you do not include FieldAttributes.Static in the attributes parameter.

Example

[Visual Basic, C#, C++] The following code sample demonstrates the use of DefineUninitializedData to create an uninitialized data field in a dynamic type:

[Visual Basic] 
Imports System
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Runtime.InteropServices


Public Class EmittedClass

   Public Shared 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 'Main


   Private Shared 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 Shared 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 Class 'EmittedClass

[C#] 
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;


public class EmittedClass
{
   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());
   }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Runtime::InteropServices;


public __gc class EmittedClass
{
public:
    static void Main()
    {
        Type* myHelloWorldType = CreateCallee(Thread::GetDomain());
        Object* myHelloWorldInstance =
            Activator::CreateInstance(myHelloWorldType);
        FieldInfo* myGreetingFieldInfo =
            myHelloWorldType->GetField(S"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(S"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.
    static Type* CreateCallee(AppDomain* myDomain)
    {
        // Create a simple name for the callee assembly.
        AssemblyName* myAssemblyName = new AssemblyName();
        myAssemblyName->Name = S"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(S"EmittedModule");

        // Define a public class named "MyHelloWorld"
        TypeBuilder* myHelloWorldType =
            myModule->DefineType(S"MyHelloWorld", TypeAttributes::Public);

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

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

int main()
{
    EmittedClass::Main();
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

TypeBuilder Class | TypeBuilder Members | System.Reflection.Emit Namespace

Show: