Export (0) Print
Expand All

TypeBuilder.DefineUninitializedData Method

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

Namespace:  System.Reflection.Emit
Assembly:  mscorlib (in mscorlib.dll)

Public Function DefineUninitializedData ( _
	name As String, _
	size As Integer, _
	attributes As FieldAttributes _
) As FieldBuilder


Type: System.String

The name used to refer to the data. name cannot contain embedded nulls.

Type: System.Int32

The size of the data field.

Type: System.Reflection.FieldAttributes

The attributes for the field.

Return Value

Type: System.Reflection.Emit.FieldBuilder
A field to reference the data.


Length of name is zero.


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


name is Nothing.


The type was previously created using CreateType.

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

The following code sample demonstrates the use of DefineUninitializedData to create an uninitialized data field in a dynamic type:

Imports System
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)

      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))
         myIntPtr = intptr.op_Explicit(myIntPtr.ToInt32 + 1)
      Next j
   End Sub 'Main

   Private Function GetRandBytes(ByVal iRandSeed As Integer, ByVal iSize As Integer) As Byte()
      Dim barr(iSize) As Byte 
      Dim randTemp As New Random(iRandSeed)
      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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

© 2014 Microsoft