/// <summary> /// Creates a new instance of the <see cref="CC_ILWriter"/> class /// </summary> /// <param name="method">The method that writes the IL code</param> public CC_ILWriter(ILWriterDelegate method) { if (method == null) { throw new ArgumentNullException(); } WriterMethod = method; }
/// <summary> /// Generates a dynamic method of the given name and signature, containing the instructions /// produced by 'ilWriter'. A delegate to the method, is returned to the caller as a native /// function pointer. /// </summary> /// <remarks> /// This method also writes the method to the 'Stubs' class in 'Dynamic.dll' for debugging /// purposes. /// </remarks> private static IntPtr GenerateDynamicMethod(string methodName, DelegateSignature methodSignature, ILWriterDelegate ilWriter) { if (true) { // Optional: generate an implementation of the stub method inside the 'Stubs' // class (which is saved to 'Dynamic.dll') for debugging purposes. MethodBuilder savedMethod = _stubsTypeBuilder.DefineMethod(methodName, MethodAttributes.Public, methodSignature.ReturnType, methodSignature.ParameterTypes); ilWriter(savedMethod.GetILGenerator()); } // Generate a dynamic method of the given name and signature, and the // IL instructions given by 'ilWriter', then return a delegate to the // method as a function pointer. // [stubReturnType] [classType]_[methodname]([classType] this, [args...]): DynamicMethod method = new DynamicMethod(methodName, methodSignature.ReturnType, methodSignature.ParameterTypes, typeof(Driver)); ilWriter(method.GetILGenerator()); return(GetDotNetFunPtrForDelegate(method.CreateDelegate(methodSignature.ToDelegateType()))); }