public static MethodBuilder DefineMethod(this TypeBuilder typeBuilder, MethodInfo method, MethodAttributes? attributes = null, ParameterInfo[] parameters = null) { Type[] parametersTypes = null; MethodBuilder methodBuilder = null; parameters = parameters ?? method.GetParameters(); parametersTypes = parameters.ToArray(parameter => parameter.ParameterType); attributes = attributes ?? method.Attributes & ~MethodAttributes.Abstract; methodBuilder = typeBuilder.DefineMethod(method.Name, attributes.Value, method.ReturnType, parametersTypes); parameters.ForEach(1, (parameter, i) => { var parameterBuilder = methodBuilder.DefineParameter(i, parameter.Attributes, parameter.Name); if (parameter.IsDefined<ParamArrayAttribute>()) { parameterBuilder.SetCustomAttribute<ParamArrayAttribute>(); } else if (parameter.IsOut) { parameterBuilder.SetCustomAttribute<OutAttribute>(); } else if (parameter.IsOptional) { parameterBuilder.SetCustomAttribute<OptionalAttribute>() .SetConstant(parameter.DefaultValue); } }); return methodBuilder; }