Example #1
0
        internal static MethodReference MakeMethodReference(this MethodReference method, TypeReference declaringType, params GenericParameter[] arguments)
        {
            var methodRef = new MethodReference(method.Name, method.ReturnType, declaringType)
            {
                HasThis           = method.HasThis,
                ExplicitThis      = method.ExplicitThis,
                CallingConvention = method.CallingConvention
            };

            methodRef.CopyGenericParameters(method.GenericParameters);

            foreach (var original in method.Parameters)
            {
                var clone = new ParameterDefinition(original.Name, original.Attributes, original.ParameterType);

                foreach (var customAttr in original.CustomAttributes)
                {
                    clone.CustomAttributes.Add(customAttr);
                }

                methodRef.Parameters.Add(clone);
            }

            if (method.GenericParameters.Count > 0)
            {
                var methodGenericRef = new GenericInstanceMethod(methodRef);

                foreach (var genericParameter in method.GenericParameters)
                {
                    var arg = arguments.Last(a => a.Name == genericParameter.Name);
                    methodGenericRef.GenericArguments.Add(arg);
                }

                methodRef = methodGenericRef;
            }

            return(methodRef);
        }