當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET FieldBuilder.Name屬性代碼示例

本文整理匯總了VB.NET中System.Reflection.Emit.FieldBuilder.Name屬性的典型用法代碼示例。如果您正苦於以下問題:VB.NET FieldBuilder.Name屬性的具體用法?VB.NET FieldBuilder.Name怎麽用?VB.NET FieldBuilder.Name使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在System.Reflection.Emit.FieldBuilder的用法示例。


在下文中一共展示了FieldBuilder.Name屬性的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。

示例1: FieldBuilder_Sample

' 導入命名空間
Imports System.Reflection
Imports System.Reflection.Emit

Public Module FieldBuilder_Sample
   Private Function CreateType() As Type
      ' Create an assembly.
      Dim assemName As New AssemblyName()
      assemName.Name = "DynamicAssembly"
      Dim assemBuilder As AssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemName,
                                                AssemblyBuilderAccess.Run)
      ' Create a dynamic module in Dynamic Assembly.
      Dim modBuilder As ModuleBuilder = assemBuilder.DefineDynamicModule("DynamicModule")
      ' Define a public class named "DynamicClass" in the assembly.
      Dim typBuilder As TypeBuilder = modBuilder.DefineType("DynamicClass", 
                                          TypeAttributes.Public)
      ' Define a private String field named "DynamicField" in the type.
      Dim fldBuilder As FieldBuilder = typBuilder.DefineField("DynamicField",
                  GetType(String), FieldAttributes.Private Or FieldAttributes.Static)
      ' Create the constructor.
      Dim constructorArgs As Type() = {GetType(String)}
      Dim constructor As ConstructorBuilder = 
                  typBuilder.DefineConstructor(MethodAttributes.Public, 
                           CallingConventions.Standard, constructorArgs)
      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, fldBuilder)
      constructorIL.Emit(OpCodes.Ret)

      ' Create the DynamicMethod method.
      Dim methBuilder As MethodBuilder = typBuilder.DefineMethod("DynamicMethod", 
                        MethodAttributes.Public, GetType(String), Nothing)
      Dim methodIL As ILGenerator = methBuilder.GetILGenerator()
      methodIL.Emit(OpCodes.Ldarg_0)
      methodIL.Emit(OpCodes.Ldfld, fldBuilder)
      methodIL.Emit(OpCodes.Ret)

      Console.WriteLine($"Name               : {fldBuilder.Name}")
      Console.WriteLine($"DeclaringType      : {fldBuilder.DeclaringType}")
      Console.WriteLine($"Type               : {fldBuilder.FieldType}")
      Return typBuilder.CreateType()
   End Function 

   Public Sub Main()
      Dim dynType As Type = CreateType()
      Try  
        ' Create an instance of the "HelloWorld" class.
         Dim helloWorld As Object = Activator.CreateInstance(dynType, New Object() {"HelloWorld"})
         ' Invoke the "DynamicMethod" method of the "DynamicClass" class.
         Dim obj As Object = dynType.InvokeMember("DynamicMethod", 
                  BindingFlags.InvokeMethod, Nothing, helloWorld, Nothing)
         Console.WriteLine($"DynamicClass.DynamicMethod returned: ""{obj}""")
      Catch e As MethodAccessException
            Console.WriteLine($"{e.GetType().Name}: {e.Message}")
      End Try
   End Sub 
End Module
開發者ID:VB.NET開發者,項目名稱:System.Reflection.Emit,代碼行數:61,代碼來源:FieldBuilder.Name


注:本文中的System.Reflection.Emit.FieldBuilder.Name屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。