The Labs \ Source Viewer \ SSCLI \ System.Reflection.Emit \ MethodRental

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. /*============================================================
  16. **
  17. ** Class:  MethodRental
  18. **
  19. **
  20. ** MethodRental class is to provide a fast way to swap method body implementation
  21. **  given a method of a class
  22. **
  23. **
  24. ===========================================================*/
  25. namespace System.Reflection.Emit
  26. {
  27.    
  28.     using System;
  29.     using System.Reflection;
  30.     using System.Threading;
  31.     using System.Runtime.CompilerServices;
  32.     using System.Security.Permissions;
  33.     using System.Runtime.InteropServices;
  34.     using System.Globalization;
  35.    
  36.     // MethodRental class provides the ability to insert a new method body of an
  37.     // existing method on a class defined in a DynamicModule.
  38.     // Can throw OutOfMemory exception.
  39.     //
  40.     //This class contains only static methods and does not require serialization.
  41.     [HostProtection(MayLeakOnAbort = true)]
  42.     [ClassInterface(ClassInterfaceType.None)]
  43.     [ComDefaultInterface(typeof(_MethodRental))]
  44.     [System.Runtime.InteropServices.ComVisible(true)]
  45.     public sealed class MethodRental : _MethodRental
  46.     {
  47.         public const int JitOnDemand = 0;
  48.         // jit the method body when it is necessary
  49.         public const int JitImmediate = 1;
  50.         // jit the method body now
  51.         [SecurityPermissionAttribute(SecurityAction.Demand, UnmanagedCode = true)]
  52.             // [in] class containing the method
  53.             // [in] method token
  54.             // [in] pointer to bytes
  55.             // [in] the size of the new method body in bytes
  56.         // [in] flags
  57.         public static void SwapMethodBody(Type cls, int methodtoken, IntPtr rgIL, int methodSize, int flags)
  58.         {
  59.             if (methodSize <= 0 || methodSize >= 4128768)
  60.                 throw new ArgumentException(Environment.GetResourceString("Argument_BadSizeForData"), "methodSize");
  61.            
  62.             if (cls == null)
  63.                 throw new ArgumentNullException("cls");
  64.            
  65.             // can only swap method body on dynamic module
  66.             if (!(cls.Module is ModuleBuilder))
  67.                 throw new NotSupportedException(Environment.GetResourceString("NotSupported_NotDynamicModule"));
  68.            
  69.             RuntimeType rType;
  70.            
  71.             if (cls is TypeBuilder) {
  72.                 // If it is a TypeBuilder, make sure that TypeBuilder is already been baked.
  73.                 TypeBuilder typeBuilder = (TypeBuilder)cls;
  74.                 if (typeBuilder.m_hasBeenCreated == false)
  75.                     throw new NotSupportedException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("NotSupported_NotAllTypesAreBaked"), typeBuilder.Name));
  76.                
  77.                 // get the corresponding runtime type for the TypeBuilder.
  78.                 rType = typeBuilder.m_runtimeType as RuntimeType;
  79.                
  80.             }
  81.             else {
  82.                 rType = cls as RuntimeType;
  83.             }
  84.            
  85.             if (rType == null)
  86.                 throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "cls");
  87.            
  88.             StackCrawlMark mark = StackCrawlMark.LookForMyCaller;
  89.            
  90.             if (rType.Assembly.m_assemblyData.m_isSynchronized) {
  91.                 lock (rType.Assembly.m_assemblyData) {
  92.                     SwapMethodBodyHelper(rType, methodtoken, rgIL, methodSize, flags, ref mark);
  93.                 }
  94.             }
  95.             else {
  96.                 SwapMethodBodyHelper(rType, methodtoken, rgIL, methodSize, flags, ref mark);
  97.             }
  98.         }
  99.        
  100.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  101.             // [in] class containing the method
  102.             // [in] method token
  103.             // [in] pointer to bytes
  104.             // [in] the size of the new method body in bytes
  105.             // [in] flags
  106.         static internal extern void SwapMethodBodyHelper(RuntimeType cls, int methodtoken, IntPtr rgIL, int methodSize, int flags, ref StackCrawlMark stackMark);
  107.         // [in] stack crawl mark used to find caller
  108.         // private constructor to prevent class to be constructed.
  109.         private MethodRental()
  110.         {
  111.         }
  112.        
  113.        
  114.         void _MethodRental.GetTypeInfoCount(out uint pcTInfo)
  115.         {
  116.             throw new NotImplementedException();
  117.         }
  118.        
  119.         void _MethodRental.GetTypeInfo(uint iTInfo, uint lcid, IntPtr ppTInfo)
  120.         {
  121.             throw new NotImplementedException();
  122.         }
  123.        
  124.         void _MethodRental.GetIDsOfNames(        [In()]
  125. ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
  126.         {
  127.             throw new NotImplementedException();
  128.         }
  129.        
  130.         void _MethodRental.Invoke(uint dispIdMember,         [In()]
  131. ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
  132.         {
  133.             throw new NotImplementedException();
  134.         }
  135.        
  136.     }
  137. }

Developer Fusion