/// <summary> /// 取得参数列表 /// </summary> /// <param name="methodName"></param> /// <param name="data"></param> /// <returns></returns> private object[] GetArguments(string methodName, Dictionary <string, object> data) { MethodInfo methInfo; if (ReflectionCache.GetInstance().Get(methodName) == null) { methInfo = this.GetType().GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public); ReflectionCache.GetInstance().Add(methodName, methInfo); } else { methInfo = ReflectionCache.GetInstance().Get(methodName); } if (methInfo == null) { throw new Exception("指定的逻辑方法不存在"); } List <object> args = new List <object>(); if (methInfo != null) { ParameterInfo[] paraInfos = methInfo.GetParameters(); foreach (ParameterInfo p in paraInfos) { object obj = data[p.Name]; if (null != obj) { args.Add(Convert.ChangeType(obj, p.ParameterType)); } } } return(args.ToArray()); }
/// <summary> /// 取得方法的返回值标注属性 /// </summary> /// <param name="methodName"></param> /// <returns></returns> private ResponseAnnotationAttribute GetAnnation(string methodName) { MethodInfo methInfo; if (ReflectionCache.GetInstance().Get(methodName) == null) { methInfo = this.GetType().GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public); ReflectionCache.GetInstance().Add(methodName, methInfo); } else { methInfo = ReflectionCache.GetInstance().Get(methodName); } if (methInfo == null) { throw new Exception("指定的逻辑方法不存在"); } ResponseAnnotationAttribute[] resAnns = (ResponseAnnotationAttribute[])methInfo.GetCustomAttributes(typeof(ResponseAnnotationAttribute), false); ResponseAnnotationAttribute ann = null; ann = resAnns.Length == 0 ? ResponseAnnotationAttribute.Default : resAnns[0]; ann = (ann as ICloneable).Clone() as ResponseAnnotationAttribute; ParameterInfo[] ps = methInfo.GetParameters(); if (ps != null) { ann.Parameter = ps.Length; } return(ann); }
public StringBuilder GetSvr() { Type type = this.GetType(); Uri url = HttpContext.Current.Request.Url; string script = string.Empty; if (!string.IsNullOrEmpty(ReflectionCache.GetInstance().JsFile)) { script = ReflectionCache.GetInstance().JsFile; } else { ReflectionCache.GetInstance().JsFile = GetScriptTemplete(); script = ReflectionCache.GetInstance().JsFile; } script = script.Replace("%H_DESC%", "通过jQuery.ajax完成服务端函数调用"); script = script.Replace("%H_DATE%", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); script = script.Replace("%URL%", url.Query.Length > 0 ? url.ToString().Replace(url.Query, "") : url.ToString()); script = script.Replace("%CLS%", type.Name); StringBuilder scriptBuilder = new StringBuilder(script); MethodInfo[] methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); foreach (MethodInfo m in methods) { ResponseAnnotationAttribute resAnn = this.GetAnnation(m.Name); scriptBuilder.AppendLine("/*"); scriptBuilder.AppendLine("功能说明:" + resAnn.MethodDescription); scriptBuilder.AppendLine("附加说明:缓存时间 " + resAnn.CacheDurtion.ToString() + " 秒"); scriptBuilder.AppendLine("输出类型 " + resAnn.ResponseFormat.ToString()); scriptBuilder.AppendLine("*/"); if (string.IsNullOrEmpty(resAnn.HttpMethod)) { resAnn.HttpMethod = "POST"; } string func = GetFunctionTemplete(m, resAnn.ResponseFormat, resAnn.HttpMethod); scriptBuilder.AppendLine(func); } return(scriptBuilder); }
/// <summary> /// 调用本地的方法 /// </summary> /// <param name="methodName"></param> /// <param name="args"></param> /// <returns></returns> private object Invoke(string methodName, Dictionary <string, object> args) { MethodInfo methInfo; if (ReflectionCache.GetInstance().Get(methodName) == null) { methInfo = this.GetType().GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public); ReflectionCache.GetInstance().Add(methodName, methInfo); } else { methInfo = ReflectionCache.GetInstance().Get(methodName); } if (methInfo == null) { throw new Exception("指定的逻辑方法不存在"); } object[] parameters = this.GetArguments(methodName, args); //object result = methInfo.Invoke(this, parameters); object result = ReflectionCache.GetMethodInvoker(methInfo).Invoke(this, parameters); return(result); }