public sealed class MethodRental
|
JitImmediate | Specifies that the method should be just-in-time (JIT) compiled immediately. |
JitOnDemand | Specifies that the method should be just-in-time (JIT) compiled when needed. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
SwapMethodBody | Swaps the body of a method. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public const int JitImmediate;
|
public const int JitOnDemand;
|
~MethodRental(); |
public virtual int GetHashCode(); |
public Type GetType(); |
protected object MemberwiseClone(); |
public static void SwapMethodBody( |
cls
methodtoken
rgIL
methodSize
flags
Exception Type | Condition |
---|---|
ArgumentNullException | cls is null. |
NotSupportedException | The type cls is not complete. |
The method can only be called by the client that created the dynamic module that contains the type whose method's body is being swapped.
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 1. 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 1" ); } 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"); } } }
public virtual string ToString(); |