public static void SwapMethodBody(Type cls, int methodtoken, IntPtr rgIL, int methodSize, int flags) { if (methodSize <= 0 || methodSize >= 4128768) { throw new ArgumentException(Environment.GetResourceString("Argument_BadSizeForData"), "methodSize"); } if (cls == null) { throw new ArgumentNullException("cls"); } Module module = cls.Module; ModuleBuilder moduleBuilder = module as ModuleBuilder; InternalModuleBuilder internalModuleBuilder; if (moduleBuilder != null) { internalModuleBuilder = moduleBuilder.InternalModule; } else { internalModuleBuilder = (module as InternalModuleBuilder); } if (internalModuleBuilder == null) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_NotDynamicModule")); } RuntimeType runtimeType; if (cls is TypeBuilder) { TypeBuilder typeBuilder = (TypeBuilder)cls; if (!typeBuilder.IsCreated()) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_NotAllTypesAreBaked", new object[] { typeBuilder.Name })); } runtimeType = typeBuilder.BakedRuntimeType; } else { runtimeType = (cls as RuntimeType); } if (runtimeType == null) { throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "cls"); } StackCrawlMark stackCrawlMark = StackCrawlMark.LookForMyCaller; RuntimeAssembly runtimeAssembly = internalModuleBuilder.GetRuntimeAssembly(); object syncRoot = runtimeAssembly.SyncRoot; lock (syncRoot) { MethodRental.SwapMethodBody(runtimeType.GetTypeHandleInternal(), methodtoken, rgIL, methodSize, flags, JitHelpers.GetStackCrawlMarkHandle(ref stackCrawlMark)); } }
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable public static void SwapMethodBody( Type cls, // [in] class containing the method int methodtoken, // [in] method token IntPtr rgIL, // [in] pointer to bytes int methodSize, // [in] the size of the new method body in bytes int flags) // [in] flags { if (methodSize <= 0 || methodSize >= 0x3f0000) throw new ArgumentException(Environment.GetResourceString("Argument_BadSizeForData"), "methodSize"); if (cls==null) throw new ArgumentNullException("cls"); Contract.EndContractBlock(); Module module = cls.Module; InternalModuleBuilder internalMB; ModuleBuilder mb = module as ModuleBuilder; if (mb != null) internalMB = mb.InternalModule; else internalMB = module as InternalModuleBuilder; // can only swap method body on dynamic module // dynamic internal module type is always exactly InternalModuleBuilder, non-dynamic is always something different if (internalMB == null) throw new NotSupportedException(Environment.GetResourceString("NotSupported_NotDynamicModule")); RuntimeType rType; if (cls is TypeBuilder) { // If it is a TypeBuilder, make sure that TypeBuilder is already been baked. TypeBuilder typeBuilder = (TypeBuilder) cls; if (!typeBuilder.IsCreated()) throw new NotSupportedException(Environment.GetResourceString("NotSupported_NotAllTypesAreBaked", typeBuilder.Name)); // get the corresponding runtime type for the TypeBuilder. rType = typeBuilder.BakedRuntimeType; } else { rType = cls as RuntimeType; } if (rType == null) throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "cls"); StackCrawlMark mark = StackCrawlMark.LookForMyCaller; RuntimeAssembly rtAssembly = internalMB.GetRuntimeAssembly(); lock (rtAssembly.SyncRoot) { SwapMethodBody(rType.GetTypeHandleInternal(), methodtoken, rgIL, methodSize, flags, JitHelpers.GetStackCrawlMarkHandle(ref mark)); } }
internal bool IsTypeCreated() { return(m_containingType != null && m_containingType.IsCreated()); }