当前位置: 首页>>代码示例>>VB.NET>>正文


VB.NET MethodBuilder类代码示例

本文整理汇总了VB.NET中System.Reflection.Emit.MethodBuilder的典型用法代码示例。如果您正苦于以下问题:VB.NET MethodBuilder类的具体用法?VB.NET MethodBuilder怎么用?VB.NET MethodBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了MethodBuilder类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。

示例1: DemoMethodBuilder

' 导入命名空间
Imports System.Reflection
Imports System.Reflection.Emit

Class DemoMethodBuilder
   
   Public Shared Sub AddMethodDynamically(ByVal myTypeBld As TypeBuilder, _
                                          ByVal mthdName As String, _
                                          ByVal mthdParams() As Type, _
                                          ByVal returnType As Type, _
                                          ByVal mthdAction As String)
      
      Dim myMthdBld As MethodBuilder = myTypeBld.DefineMethod(mthdName, _
                                       MethodAttributes.Public Or MethodAttributes.Static, _
                                       returnType, _
                                       mthdParams)
      
      Dim ILout As ILGenerator = myMthdBld.GetILGenerator()
      
      Dim numParams As Integer = mthdParams.Length
      
      Dim x As Byte
      For x = 0 To numParams - 1
         ILout.Emit(OpCodes.Ldarg_S, x)
      Next x
      
      If numParams > 1 Then
         Dim y As Integer
         For y = 0 To (numParams - 1) - 1
            Select Case mthdAction
               Case "A"
                  ILout.Emit(OpCodes.Add)
               Case "M"
                  ILout.Emit(OpCodes.Mul)
               Case Else
                  ILout.Emit(OpCodes.Add)
            End Select
         Next y
      End If
      ILout.Emit(OpCodes.Ret)
   End Sub 
    
   
   Public Shared Sub Main()
      
      Dim myDomain As AppDomain = AppDomain.CurrentDomain
      Dim asmName As New AssemblyName()
      asmName.Name = "MyDynamicAsm"
      
      Dim myAsmBuilder As AssemblyBuilder = myDomain.DefineDynamicAssembly(asmName, _
                                            AssemblyBuilderAccess.RunAndSave)
      
      Dim myModule As ModuleBuilder = myAsmBuilder.DefineDynamicModule("MyDynamicAsm", _
                                                                       "MyDynamicAsm.dll")
      
      Dim myTypeBld As TypeBuilder = myModule.DefineType("MyDynamicType", TypeAttributes.Public)
      
      ' Get info from the user to build the method dynamically.
      Console.WriteLine("Let's build a simple method dynamically!")
      Console.WriteLine("Please enter a few numbers, separated by spaces.")
      Dim inputNums As String = Console.ReadLine()
      Console.Write("Do you want to [A]dd (default) or [M]ultiply these numbers? ")
      Dim myMthdAction As String = Console.ReadLine().ToUpper()
      Console.Write("Lastly, what do you want to name your new dynamic method? ")
      Dim myMthdName As String = Console.ReadLine()
      
      ' Process inputNums into an array and create a corresponding Type array 
      Dim index As Integer = 0
      Dim inputNumsList As String() = inputNums.Split()
      
      Dim myMthdParams(inputNumsList.Length - 1) As Type
      Dim inputValsList(inputNumsList.Length - 1) As Object
      
      
      Dim inputNum As String
      For Each inputNum In  inputNumsList
         inputValsList(index) = CType(Convert.ToInt32(inputNum), Object)
         myMthdParams(index) = GetType(Integer)
         index += 1
      Next inputNum
      
      ' Now, call the method building method with the parameters, passing the 
      ' TypeBuilder by reference.
      AddMethodDynamically(myTypeBld, myMthdName, myMthdParams, GetType(Integer), myMthdAction)
      
      Dim myType As Type = myTypeBld.CreateType()
     
      Dim description as String 
      If myMthdAction = "M" Then
         description = "multiplying"
      Else
         description = "adding"
      End If

      Console.WriteLine("---")
      Console.WriteLine("The result of {0} the values is: {1}", _
                         description, _
                         myType.InvokeMember(myMthdName, _
                                             BindingFlags.InvokeMethod _
                                               Or BindingFlags.Public _
                                               Or BindingFlags.Static, _
                                             Nothing, _
                                             Nothing, _
                                             inputValsList)) 
      Console.WriteLine("---")

      ' If you are interested in seeing the MSIL generated dynamically for the method
      ' your program generated, change to the directory where you ran the compiled
      ' code sample and type "ildasm MyDynamicAsm.dll" at the prompt. When the list
      ' of manifest contents appears, click on "MyDynamicType" and then on the name of
      ' of the method you provided during execution.
 
      myAsmBuilder.Save("MyDynamicAsm.dll") 

      Dim myMthdInfo As MethodInfo = myType.GetMethod(myMthdName)
      Console.WriteLine("Your Dynamic Method: {0};", myMthdInfo.ToString())
   End Sub 
End Class
开发者ID:VB.NET开发者,项目名称:System.Reflection.Emit,代码行数:118,代码来源:MethodBuilder


注:本文中的System.Reflection.Emit.MethodBuilder类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。