/// <summary> /// 执行,按实际结果返回 /// </summary> /// <param name="letter">客户端递交的参数信息</param> /// <returns></returns> public static object Exec(Letter letter) { //1.创建对象,即$api.get("account/single")中的account object execObj = ExecuteMethod.CreateInstance(letter); //2.获取要执行的方法,即$api.get("account/single")中的single MethodInfo method = getMethod(execObj.GetType(), letter); //3#.验证方法的特性,一是验证Http动词,二是验证是否登录后操作,三是验证权限 //----验证Http谓词访问限制 HttpAttribute.Verify(letter.HTTP_METHOD, method); //LoginAttribute.Verify(method); //----范围控制,本机或局域网,或同域 bool isRange = RangeAttribute.Verify(letter, method); //----验证是否需要登录 LoginAttribute loginattr = LoginAttribute.Verify(method); //4.构建执行该方法所需要的参数 object[] parameters = getInvokeParam(method, letter); //5.执行方法,返回结果 object objResult = method.Invoke(execObj, parameters); //将执行结果写入日志 LoginAttribute.LogWrite(loginattr, objResult); return(objResult); }
/// <summary> /// 执行,按实际结果返回 /// </summary> /// <param name="letter">客户端递交的参数信息</param> /// <returns></returns> public static object Exec(Letter letter) { //1.创建对象,即$api.get("account/single")中的account IViewAPI execObj = ExecuteMethod.CreateInstance(letter); //2.获取要执行的方法,即$api.get("account/single")中的single MethodInfo method = getMethod(execObj.GetType(), letter); //清除缓存 if (letter.HTTP_METHOD.Equals("put", StringComparison.CurrentCultureIgnoreCase)) { CacheAttribute.Remove(method, letter); } //3#.验证方法的特性,一是验证Http动词,二是验证是否登录后操作,三是验证权限 //----验证Http谓词访问限制 HttpAttribute.Verify(letter.HTTP_METHOD, method); //LoginAttribute.Verify(method); //----范围控制,本机或局域网,或同域 bool isRange = RangeAttribute.Verify(method, letter); //----验证是否需要登录 LoginAttribute loginattr = LoginAttribute.Verify(method, letter); //----清理参数值中的html标签,默认全部清理,通过设置not参数不过虑某参数 HtmlClearAttribute.Clear(method, letter); //4.构建执行该方法所需要的参数 object[] parameters = getInvokeParam(method, letter); //5.执行方法,返回结果 object objResult = null; //结果 //只有get方式时,才使用缓存 CacheAttribute cache = null; //if (letter.HTTP_METHOD.Equals("put", StringComparison.CurrentCultureIgnoreCase)) // CacheAttribute.Remove(method, letter); if (letter.HTTP_METHOD.Equals("get", StringComparison.CurrentCultureIgnoreCase)) { cache = CacheAttribute.GetAttr <CacheAttribute>(method); } if (cache != null) { objResult = CacheAttribute.GetResult(method, letter); if (objResult == null) { objResult = method.Invoke(execObj, parameters); CacheAttribute.Insert(cache.Expires, method, letter, objResult); } } else { objResult = method.Invoke(execObj, parameters); } //将执行结果写入日志 LoginAttribute.LogWrite(loginattr, objResult); return(objResult); }
/// <summary> /// 创建对象,如果存在,则直接返回;如果不存在,创建后返回 /// </summary> /// <param name="letter"></param> /// <returns></returns> public static IViewAPI CreateInstance(Letter letter) { string assemblyName = "Song.ViewData"; string classFullName = String.Format("{0}.Methods.{1}", assemblyName, letter.ClassName); IViewAPI obj = null; //由缓存中查找,是否存在 ExecuteMethod curr = ExecuteMethod.GetInstance(); foreach (KeyValuePair <string, object> kv in curr._objects) { if (classFullName.Trim().Equals(kv.Key, StringComparison.CurrentCultureIgnoreCase)) { obj = (IViewAPI)kv.Value; break; } } if (obj != null) { return(obj); } //如果之前未创建,则重新创建 Type type = null; Assembly assembly = Assembly.Load(assemblyName); Type[] types = assembly.GetExportedTypes() .Where(t => t.GetInterfaces().Contains(typeof(IViewAPI))) .ToArray(); foreach (Type info in types) { if (info.FullName.Equals(classFullName, StringComparison.CurrentCultureIgnoreCase)) { type = info; break; } } if (type == null) { throw new Exception( string.Format("调用的接口'{0}'不存在, 可能是'{1}'拼写错误", letter.API_PATH, letter.ClassName)); } obj = (IViewAPI)System.Activator.CreateInstance(type); //创建对象 //记录到缓存 if (!ExecuteMethod.GetInstance()._objects.ContainsKey(type.FullName)) { ExecuteMethod.GetInstance()._objects.Add(type.FullName, obj); } return(obj); }
/// <summary> /// 执行,按实际结果返回 /// </summary> /// <param name="letter">客户端递交的参数信息</param> /// <returns></returns> public static object Exec(Letter letter) { //1.创建对象,即$api.get("account/single")中的account object execObj = ExecuteMethod.CreateInstance(letter); //2.获取要执行的方法,即$api.get("account/single")中的single MethodInfo method = getMethod(execObj.GetType(), letter); //3#.验证方法的特性,一是验证Http动词,二是验证是否登录后操作,三是验证权限 //4.构建执行该方法所需要的参数 object[] parameters = getInvokeParam(method, letter); //5.执行方法 return(method.Invoke(execObj, parameters)); }
/// <summary> /// 执行,按实际结果返回 /// </summary> /// <param name="letter">客户端递交的参数信息</param> /// <returns></returns> public static object Exec(Letter letter) { //1.创建对象,即$api.get("account/single")中的account object execObj = ExecuteMethod.CreateInstance(letter); //2.获取要执行的方法,即$api.get("account/single")中的single MethodInfo method = getMethod(execObj.GetType(), letter); //3#.验证方法的特性,一是验证Http动词,二是验证是否登录后操作,三是验证权限 //----验证Http谓词访问限制 HttpAttribute.Verify(letter.HTTP_METHOD, method); //LoginAttribute.Verify(method); //----范围控制,本机或局域网,或同域 bool isRange = RangeAttribute.Verify(letter, method); //----验证是否需要登录 LoginAttribute loginattr = LoginAttribute.Verify(method); //4.构建执行该方法所需要的参数 object[] parameters = getInvokeParam(method, letter); //5.执行方法,返回结果 object objResult = null; //结果 //只有get方式时,才使用缓存 CacheAttribute cache = null; if (letter.HTTP_METHOD == "GET") { cache = CacheAttribute.GetAttr <CacheAttribute>(method); } if (cache != null) { objResult = CacheAttribute.GetResult(method, letter); if (objResult == null) { objResult = method.Invoke(execObj, parameters); CacheAttribute.Insert(cache.Expires, method, letter, objResult); } } else { objResult = method.Invoke(execObj, parameters); } //将执行结果写入日志 LoginAttribute.LogWrite(loginattr, objResult); return(objResult); }
/// <summary> /// 创建对象,如果存在,则直接返回;如果不存在,创建后返回 /// </summary> /// <param name="letter"></param> /// <returns></returns> public static object CreateInstance(Letter letter) { string assemblyName = "Song.ViewData"; string classFullName = String.Format("{0}.Methods.{1}", assemblyName, letter.ClassName); object obj = null; //由缓存中查找,是否存在 ExecuteMethod curr = ExecuteMethod.GetInstance(); foreach (KeyValuePair <string, object> kv in curr._objects) { if (classFullName.Trim().Equals(kv.Key, StringComparison.CurrentCultureIgnoreCase)) { obj = kv.Value; break; } } if (obj != null) { return(obj); } //如果之前未创建,则重新创建 Type type = null; Assembly assembly = Assembly.Load(assemblyName); foreach (Type info in assembly.GetTypes()) { if (info.FullName.Equals(classFullName, StringComparison.CurrentCultureIgnoreCase)) { type = info; break; } } if (type == null) { throw new Exception( string.Format("调用的对象'{0}'不存在, 可能是'{1}'拼写错误", classFullName, letter.ClassName)); } obj = System.Activator.CreateInstance(type); //创建对象 ExecuteMethod.GetInstance()._objects.Add(type.FullName, obj); //记录到缓存 return(obj); }