Example #1
0
        /// <summary>
        /// Emits the definition of a delegate into the assembly based on the definition of a local method.
        /// </summary>
        /// <remarks>Based on code from http://markmail.org/thread/rqbgst2kqgdv33uy#query:related%3Arqbgst2kqgdv33uy+page:1+mid:rqbgst2kqgdv33uy+state:results. </remarks>
        /// <param name="processor">The IL processor on which to define the delegate in.</param>
        /// <param name="sourceMethod">The local method on which to base the delegate.</param>
        /// <param name="delegateVariable">The delegate variable to define (in the local method).</param>
        /// <param name="delegateCtor">The delegate constructor that will be used to create new instances of the delegate.</param>
        public static TypeDefinition EmitDelegate(ILProcessor processor, TypeDefinition sourceType, MethodDefinition sourceMethod, TypeReference originalType, out VariableDefinition delegateVariable, out MethodReference delegateCtor)
        {
            var delegateEmitter = new DefaultDelegateEmitter();
            var definition      = delegateEmitter.EmitDelegate(processor.Body.Method, out delegateCtor);

            var type_MulticastDelegate = processor.Body.Method.Module.Import(typeof(MulticastDelegate));

            delegateVariable = new VariableDefinition("d", type_MulticastDelegate);
            processor.Body.Variables.Insert(0, delegateVariable);

            return(definition);
        }
Example #2
0
        /// <summary>
        /// Emits the definition of a delegate into the assembly based on the definition of a local method.
        /// </summary>
        /// <remarks>Based on code from http://markmail.org/thread/rqbgst2kqgdv33uy#query:related%3Arqbgst2kqgdv33uy+page:1+mid:rqbgst2kqgdv33uy+state:results. </remarks>
        /// <param name="processor">The IL processor on which to define the delegate in.</param>
        /// <param name="sourceMethod">The local method on which to base the delegate.</param>
        /// <param name="delegateVariable">The delegate variable to define (in the local method).</param>
        /// <param name="delegateCtor">The delegate constructor that will be used to create new instances of the delegate.</param>
        public static TypeDefinition EmitDelegate(ILProcessor processor, TypeDefinition sourceType, MethodDefinition sourceMethod, TypeReference originalType, out VariableDefinition delegateVariable, out MethodReference delegateCtor)
        {
            var delegateEmitter = new DefaultDelegateEmitter();
            var definition = delegateEmitter.EmitDelegate(processor.Body.Method, out delegateCtor);

            var type_MulticastDelegate = processor.Body.Method.Module.Import(typeof(MulticastDelegate));
            delegateVariable = new VariableDefinition("d", type_MulticastDelegate);
            processor.Body.Variables.Insert(0, delegateVariable);

            return definition;
        }