internal static void GetMethodDetails(MethodInfo method, out string defaultMethodName, out string otherMethodName) { string camelCasedMethodName = method.Name.CamelCase(); string pascalCasedMethodName = method.Name.PascalCase(); defaultMethodName = camelCasedMethodName; otherMethodName = pascalCasedMethodName; MethodCase methodCase = GetMethodCase(method.DeclaringType.GetCustomAttributeOfType <ProxyAttribute>()); switch (methodCase) { case MethodCase.Invalid: throw new InvalidOperationException("Invalid MethodCase specified"); case MethodCase.CamelCase: defaultMethodName = camelCasedMethodName; otherMethodName = pascalCasedMethodName; break; case MethodCase.PascalCase: defaultMethodName = pascalCasedMethodName; otherMethodName = camelCasedMethodName; break; case MethodCase.Both: break; default: break; } }
internal static MethodCase GetMethodCase(ProxyAttribute proxyAttr) { MethodCase methodCase = MethodCase.Both; if (proxyAttr != null) { methodCase = proxyAttr.MethodCase; } return(methodCase); }
internal static string GetMethodCall(Type type, MethodInfo method) { StringBuilder builder = new StringBuilder(); System.Reflection.ParameterInfo[] parameterInfos = method.GetParameters(); string defaultMethodName; string otherMethodName; GetMethodDetails(method, out defaultMethodName, out otherMethodName); string parameters = parameterInfos.ToArray().ToDelimited((pi) => pi.Name); string comma = parameterInfos.Length > 0 ? ", " : ""; builder.AppendFormat("\tb.{0}.{1} = function({2}{3}options)", type.Name, defaultMethodName, parameters, comma); builder.AppendLine("{"); string methodName = "invoke"; if (type.HasCustomAttributeOfType <EncryptAttribute>()) { methodName = "secureInvoke"; } if (type.HasCustomAttributeOfType <ApiKeyRequiredAttribute>() || method.HasCustomAttributeOfType <ApiKeyRequiredAttribute>()) { builder.Append("\t\toptions = $.extend({}, {apiKeyRequired: true}, options);"); } builder.AppendFormat("\t\treturn b.{0}('{1}', '{2}', [{3}], options != null ? (options.format == null ? 'json': options.format) : 'json', $.isFunction(options) ? {4} : options);\r\n", methodName, type.Name, method.Name, parameters, "{success: options}"); builder.AppendLine("\t};"); MethodCase methodCase = GetMethodCase(type.GetCustomAttributeOfType <ProxyAttribute>()); if (methodCase == MethodCase.Both) { builder.AppendFormat("\tb.{0}.{1} = b.{0}.{2};", type.Name, otherMethodName, defaultMethodName); builder.AppendLine(); } return(builder.ToString()); }