/// <summary>
 /// Initializes a new instance of the <see cref="CilGenericMethod"/> class.
 /// </summary>
 /// <param name="module">The module.</param>
 /// <param name="genericMethod">The generic method.</param>
 /// <param name="signature">The signature.</param>
 /// <param name="declaringType">Type of the declaring.</param>
 public CilGenericMethod(ITypeModule module, CilRuntimeMethod genericMethod, MethodSignature signature, RuntimeType declaringType)
     : base(module, genericMethod.Token, declaringType)
 {
     this.Signature = signature;
     this.Attributes = genericMethod.Attributes;
     this.ImplAttributes = genericMethod.ImplAttributes;
     this.Rva = genericMethod.Rva;
     this.Parameters = genericMethod.Parameters;
     base.Name = genericMethod.Name;
 }
        /// <summary>
        /// Generates the and replace invoke with return method.
        /// </summary>
        /// <param name="type">The type.</param>
        private void GenerateAndReplaceInvokeWithReturnMethod(RuntimeType type)
        {
            RuntimeParameter[] parameters = new RuntimeParameter[type.Methods[1].Parameters.Count];
            type.Methods[1].Parameters.CopyTo(parameters, 0);
            var stubInvoke = delegateStub.Methods[2];

            var invokeMethod = new CilRuntimeMethod(delegateStub.Module, InvokeMethodName,
                type.Methods[1].Signature, stubInvoke.Token, type, stubInvoke.Attributes, stubInvoke.ImplAttributes, stubInvoke.Rva);
            foreach (var parameter in parameters)
                invokeMethod.Parameters.Add(parameter);

            SearchAndReplaceMethod(type, InvokeMethodName, invokeMethod);
        }
        /// <summary>
        /// Generates the and replace end invoke method.
        /// </summary>
        /// <param name="type">The type.</param>
        private void GenerateAndReplaceEndInvokeMethod(RuntimeType type)
        {
            RuntimeParameter[] parameters = new RuntimeParameter[type.Methods[3].Parameters.Count];
            type.Methods[3].Parameters.CopyTo(parameters, 0);
            var stubMethod = delegateStub.Methods[3];

            var method = new CilRuntimeMethod(delegateStub.Module, EndInvokeMethodName,
                type.Methods[3].Signature, stubMethod.Token, type, stubMethod.Attributes, stubMethod.ImplAttributes, stubMethod.Rva);

            foreach (var parameter in parameters)
                method.Parameters.Add(parameter);

            SearchAndReplaceMethod(type, EndInvokeMethodName, method);
        }
        /// <summary>
        /// Generates and replace constructor.
        /// </summary>
        /// <param name="type">The type.</param>
        private void GenerateAndReplaceConstructor(RuntimeType type)
        {
            RuntimeParameter[] parameters = new RuntimeParameter[type.Methods[0].Parameters.Count];
            type.Methods[0].Parameters.CopyTo(parameters, 0);
            var stubConstructor = delegateStub.Methods[0];

            var constructor = new CilRuntimeMethod(delegateStub.Module, ConstructorName,
                type.Methods[0].Signature, stubConstructor.Token, type, stubConstructor.Attributes, stubConstructor.ImplAttributes, stubConstructor.Rva);

            foreach (var parameter in parameters)
                constructor.Parameters.Add(parameter);

            SearchAndReplaceMethod(type, ConstructorName, constructor);
        }