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


VB.NET MethodRental.SwapMethodBody方法代码示例

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


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

示例1: SwapMethodBodySample

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

Class SwapMethodBodySample
    
    ' First make a method that returns 0.
    ' Then swap the method body with a body that returns 1.
    Public Shared Sub Main()
        ' Construct a dynamic assembly
        Dim g As Guid = Guid.NewGuid()
        Dim asmname As New AssemblyName()
        asmname.Name = "tempfile" + g.ToString()
        Dim asmbuild As AssemblyBuilder = _
           System.Threading.Thread.GetDomain().DefineDynamicAssembly _
           (asmname, AssemblyBuilderAccess.Run)
        
        ' Add a dynamic module that contains one type that has one method that
        ' has no arguments.
        Dim modbuild As ModuleBuilder = asmbuild.DefineDynamicModule("test")
        Dim tb As TypeBuilder = modbuild.DefineType("name of the Type")
        Dim somemethod As MethodBuilder = _
           tb.DefineMethod("My method Name", _
           MethodAttributes.Public Or(MethodAttributes.Static), _
           GetType(Integer), New Type() {})
        ' Define the body of the method to return 0.
        Dim ilg As ILGenerator = somemethod.GetILGenerator()
        ilg.Emit(OpCodes.Ldc_I4_0)
        ilg.Emit(OpCodes.Ret)
        
        ' Complete the type and verify that it returns 0.
        Dim tbBaked As Type = tb.CreateType()
        Dim res1 As Integer = _
           CInt(tbBaked.GetMethod("My method Name").Invoke _
           (Nothing, New Object() {}))
        If res1 <> 0 Then
            Console.WriteLine("Err_001a, should have returned 0")
        Else
            Console.WriteLine("Original method returned 0")
        End If
        
        ' Define a new method body that will return a 1 instead.
        Dim methodBytes As Byte() = _
        {&H3, &H30, &HA, &H0, &H2, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H17, &H2A}
        '&H2     code size
        '&H17    ldc_i4_1
        '&H2A    ret
        
        ' Get the token for the method whose body you are replacing.
        Dim somemethodToken As MethodToken = somemethod.GetToken()
        
        ' Get the pointer to the method body.
        Dim hmem As GCHandle = _
           GCHandle.Alloc(CType(methodBytes, Object), GCHandleType.Pinned)
        Dim addr As IntPtr = hmem.AddrOfPinnedObject()
        Dim cbSize As Integer = methodBytes.Length
        
        ' Swap the old method body with the new body.
        MethodRental.SwapMethodBody(tbBaked, somemethodToken.Token, addr, _
           cbSize, MethodRental.JitImmediate)
        
        ' Verify that the modified method returns 1.
        Dim res2 As Integer = _
           CInt(tbBaked.GetMethod("My method Name").Invoke _
           (Nothing, New Object() {}))
        If res2 <> 1 Then
            Console.WriteLine("Err_001b, should have returned 1")
        Else
            Console.WriteLine("Swapped method body returned 1")
        End If
    End Sub
End Class
开发者ID:VB.NET开发者,项目名称:System.Reflection.Emit,代码行数:73,代码来源:MethodRental.SwapMethodBody


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