MethodRental.SwapMethodBody 方法
交换方法体。
**命名空间:**System.Reflection.Emit
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Public Shared Sub SwapMethodBody ( _
cls As Type, _
methodtoken As Integer, _
rgIL As IntPtr, _
methodSize As Integer, _
flags As Integer _
)
用法
Dim cls As Type
Dim methodtoken As Integer
Dim rgIL As IntPtr
Dim methodSize As Integer
Dim flags As Integer
MethodRental.SwapMethodBody(cls, methodtoken, rgIL, methodSize, flags)
public static void SwapMethodBody (
Type cls,
int methodtoken,
IntPtr rgIL,
int methodSize,
int flags
)
public:
static void SwapMethodBody (
Type^ cls,
int methodtoken,
IntPtr rgIL,
int methodSize,
int flags
)
public static void SwapMethodBody (
Type cls,
int methodtoken,
IntPtr rgIL,
int methodSize,
int flags
)
public static function SwapMethodBody (
cls : Type,
methodtoken : int,
rgIL : IntPtr,
methodSize : int,
flags : int
)
参数
- cls
包含方法的类。
- methodtoken
方法的标记。
- rgIL
指向方法的指针。它应包含方法头。
- methodSize
新方法体的大小(以字节为单位)。
- flags
控制交换的标志。请参见常数的定义。
异常
异常类型 | 条件 |
---|---|
cls 为 空引用(在 Visual Basic 中为 Nothing)。 |
|
cls 类型不完整。 |
|
methodSize 小于 1,或大于 4128767(十六进制 3effff)。 |
备注
不能使用此方法来交换全局方法体。
此方法仅可以由创建动态模块的客户端调用,该动态模块包含正在交换其方法体的类型。
示例
下面的示例阐释如何将方法体更换为新体。它还阐释如何获取现有方法的方法标记以及如何构造代表要传递给 SwapMethodBody 的 Microsoft 中间语言 (MSIL) 代码的字节的 Blob。
Imports System
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
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
class SwapMethodBodySample
{
// First make a method that returns 0.
// Then swap the method body with a body that returns 1.
public static void Main(String [] args)
{
// Construct a dynamic assembly
Guid g = Guid.NewGuid();
AssemblyName asmname = new AssemblyName();
asmname.Name = "tempfile" + g;
AssemblyBuilder asmbuild = System.Threading.Thread.GetDomain().
DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run);
// Add a dynamic module that contains one type that has one method that
// has no arguments.
ModuleBuilder modbuild = asmbuild.DefineDynamicModule( "test");
TypeBuilder tb = modbuild.DefineType( "name of the Type" );
MethodBuilder somemethod = tb.DefineMethod
("My method Name",
MethodAttributes.Public | MethodAttributes.Static,
typeof(int),
new Type[]{} );
// Define the body of the method to return 0.
ILGenerator ilg = somemethod.GetILGenerator();
ilg.Emit(OpCodes.Ldc_I4_0);
ilg.Emit(OpCodes.Ret);
// Complete the type and verify that it returns 0.
Type tbBaked = tb.CreateType();
int res1 = (int)tbBaked.GetMethod("My method Name").Invoke( null, new Object[]{} );
if ( res1 != 0 ) {
Console.WriteLine( "Err_001a, should have returned 0" );
} else {
Console.WriteLine("Original method returned 0");
}
// Define a new method body that will return a 1 instead.
Byte[] methodBytes = {
0x03,
0x30,
0x0A,
0x00,
0x02, // code size
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x17, // ldc_i4_1
0x2a // ret
};
// Get the token for the method whose body you are replacing.
MethodToken somemethodToken = somemethod.GetToken();
// Get the pointer to the method body.
GCHandle hmem = GCHandle.Alloc((Object) methodBytes, GCHandleType.Pinned);
IntPtr addr = hmem.AddrOfPinnedObject();
int cbSize = 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.
int res2 = (int)tbBaked.GetMethod("My method Name").Invoke( null, new Object[]{} );
if ( res2 != 1 ) {
Console.WriteLine( "Err_001b, should have returned 1" );
} else {
Console.WriteLine("Swapped method body returned 1");
}
}
}
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Runtime::InteropServices;
// First make a method that returns 0.
// Then swap the method body with a body that returns 1.
int main()
{
// Construct a dynamic assembly
Guid g = Guid::NewGuid();
AssemblyName^ asmname = gcnew AssemblyName;
asmname->Name = String::Concat( "tempfile", g );
AssemblyBuilder^ asmbuild = System::Threading::Thread::GetDomain()->DefineDynamicAssembly( asmname, AssemblyBuilderAccess::Run );
// Add a dynamic module that contains one type that has one method that
// has no arguments.
ModuleBuilder^ modbuild = asmbuild->DefineDynamicModule( "test" );
TypeBuilder^ tb = modbuild->DefineType( "name of the Type" );
array<Type^>^temp2;
MethodBuilder^ somemethod = tb->DefineMethod( "My method Name", static_cast<MethodAttributes>(MethodAttributes::Public | MethodAttributes::Static), int::typeid, temp2 );
// Define the body of the method to return 0.
ILGenerator^ ilg = somemethod->GetILGenerator();
ilg->Emit( OpCodes::Ldc_I4_0 );
ilg->Emit( OpCodes::Ret );
// Complete the type and verify that it returns 0.
Type^ tbBaked = tb->CreateType();
array<Object^>^temp0;
int res1 = safe_cast<Int32>(tbBaked->GetMethod( "My method Name" )->Invoke( nullptr, temp0 ));
if ( res1 != 0 )
{
Console::WriteLine( "Err_001a, should have returned 0" );
}
else
{
Console::WriteLine( "Original method returned 0" );
}
// Define a new method body that will return a 1 instead.
// code size
// ldc_i4_1
// ret
array<Byte>^methodBytes = {0x03,0x30,0x0A,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x17,0x2a};
// Get the token for the method whose body you are replacing.
MethodToken somemethodToken = somemethod->GetToken();
// Get the pointer to the method body.
GCHandle hmem = GCHandle::Alloc( (Object^)methodBytes, GCHandleType::Pinned );
IntPtr addr = hmem.AddrOfPinnedObject();
int cbSize = 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.
array<Object^>^temp1;
int res2 = safe_cast<Int32>(tbBaked->GetMethod( "My method Name" )->Invoke( nullptr, temp1 ));
if ( res2 != 1 )
{
Console::WriteLine( "Err_001b, should have returned 1" );
}
else
{
Console::WriteLine( "Swapped method body returned 1" );
}
}
.NET Framework 安全性
- SecurityPermission 用于执行非托管代码。关联的枚举:SecurityPermissionFlag.UnmanagedCode。
平台
Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0