本文整理汇总了VB.NET中System.Reflection.Emit.TypeBuilder.DefineTypeInitializer方法的典型用法代码示例。如果您正苦于以下问题:VB.NET TypeBuilder.DefineTypeInitializer方法的具体用法?VB.NET TypeBuilder.DefineTypeInitializer怎么用?VB.NET TypeBuilder.DefineTypeInitializer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.Emit.TypeBuilder
的用法示例。
在下文中一共展示了TypeBuilder.DefineTypeInitializer方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: MyApplication
Public Class MyApplication
Public Shared Sub Main()
' Create the "HelloWorld" class
Dim helloWorldClass As TypeBuilder = CreateCallee(Thread.GetDomain())
Console.WriteLine("Full Name : " + helloWorldClass.FullName)
Console.WriteLine("Constructors :")
Dim info As ConstructorInfo() = helloWorldClass.GetConstructors(BindingFlags.Public Or _
BindingFlags.Instance)
Dim index As Integer
For index = 0 To info.Length - 1
Console.WriteLine(info(index).ToString())
Next index
End Sub
' Create the callee transient dynamic assembly.
Private Shared Function CreateCallee(myDomain As AppDomain) As TypeBuilder
Dim myAssemblyName As New AssemblyName()
myAssemblyName.Name = "EmittedAssembly"
' Create the callee dynamic assembly.
Dim myAssembly As AssemblyBuilder = myDomain.DefineDynamicAssembly(myAssemblyName, _
AssemblyBuilderAccess.Run)
' Create a dynamic module named "CalleeModule" in the callee assembly.
Dim myModule As ModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule")
' Define a public class named "HelloWorld" in the assembly.
Dim helloWorldClass As TypeBuilder = myModule.DefineType("HelloWorld", TypeAttributes.Public)
' Define a private String field named "Greeting" in the type.
Dim greetingField As FieldBuilder = helloWorldClass.DefineField("Greeting", GetType(String), _
FieldAttributes.Private)
' Create the constructor.
Dim constructor As ConstructorBuilder = helloWorldClass.DefineTypeInitializer()
' Generate IL for the method. The constructor calls its base class
' constructor. The constructor stores its argument in the private field.
Dim constructorIL As ILGenerator = constructor.GetILGenerator()
constructorIL.Emit(OpCodes.Ldarg_0)
Dim superConstructor As ConstructorInfo = GetType(Object).GetConstructor(New Type() {})
constructorIL.Emit(OpCodes.Call, superConstructor)
constructorIL.Emit(OpCodes.Ldarg_0)
constructorIL.Emit(OpCodes.Ldarg_1)
constructorIL.Emit(OpCodes.Stfld, greetingField)
constructorIL.Emit(OpCodes.Ret)
helloWorldClass.CreateType()
Return helloWorldClass
End Function 'CreateCallee
End Class