System.Reflection.Emit.MethodRental Class

Assembly: Mscorlib.dll
Namespace: System.Reflection.Emit
Summary
Provides a fast way to swap method body implementation given a method of a class.
C# Syntax:
public sealed class MethodRental
Thread Safety
Reflection Emit is thread-safe when using assemblies that were created with the AppDomain.DefineDynamicAssembly method with the Boolean parameter isSynchronized set to true.
See also:
System.Reflection.Emit Namespace

System.Reflection.Emit.MethodRental Member List:

Public Fields
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.
Public Methods
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.
Protected Methods
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:


System.Reflection.Emit.MethodRental Member Details

Field: JitImmediate
Summary
Specifies that the method should be just-in-time (JIT) compiled immediately.
C# Syntax:
public const int JitImmediate;

Return to top


Field: JitOnDemand
Summary
Specifies that the method should be just-in-time (JIT) compiled when needed.
C# Syntax:
public const int JitOnDemand;

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~MethodRental();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: SwapMethodBody(
   Type cls,
   int methodtoken,
   IntPtr rgIL,
   int methodSize,
   int flags
)
Summary
Swaps the body of a method.
C# Syntax:
public static void SwapMethodBody(
   Type cls,
   int methodtoken,
   IntPtr rgIL,
   int methodSize,
   int flags
);
Parameters:

cls

The class containing the method.

methodtoken

The token for the method.

rgIL

A pointer to the method. This should include the method header.

methodSize

The size of the new method body in bytes.

flags

Flags that control the swapping. See the definitions of the constants.

Exceptions
Exception Type Condition
ArgumentNullException cls is null.
NotSupportedException The type cls is not complete.
Remarks
You cannot use this method to swap the body of a global method.

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.

Example
The following example illustrates how to swap a method body for a new body. It also illustrates how to obtain a method token for an existing method and how to construct a blob of bytes representing the Microsoft Intermediate Language (MSIL) code to be passed to SwapMethodBody.
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");
    }
    }
}

    

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.