本文整理匯總了VB.NET中System.Reflection.Emit.TypeBuilder.DefineUninitializedData方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET TypeBuilder.DefineUninitializedData方法的具體用法?VB.NET TypeBuilder.DefineUninitializedData怎麽用?VB.NET TypeBuilder.DefineUninitializedData使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Reflection.Emit.TypeBuilder
的用法示例。
在下文中一共展示了TypeBuilder.DefineUninitializedData方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: 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