/// <summary> /// 得到方法的最基本的信息 /// </summary> /// <returns></returns> public static CustomMethodInfo GetMethodBaseInfo(MethodPathInfo methodPathInfo, BindingFlags bindingAttr) { CustomMethodInfo customMethodInfo = new CustomMethodInfo(); try { //得到程序集 customMethodInfo.Assembly = Assembly.Load(methodPathInfo.Assembly); //得到类的类型 Type t = customMethodInfo.Assembly.GetType(methodPathInfo.Assembly + "." + methodPathInfo.ClassName, true, true); //得到类的类型 customMethodInfo.ClassType = t; //得到方法 customMethodInfo.Method = t.GetMethod(methodPathInfo.MethodName, bindingAttr); if (customMethodInfo.Method == null) { //没有得到方法 把整个信息置空 customMethodInfo = null; } } catch { throw; } return(customMethodInfo); }
/// <summary> /// 添加一个方法的缓存 /// </summary> /// <param name="key">方法对应的键值</param> /// <param name="customMethodInfo">缓存中的实体信息</param> internal static void AddMethodCache(string key, CustomMethodInfo customMethodInfo) { #region 初始化方法的一些信息 //特性列表 customMethodInfo.AttrList = ReflectionHelper.GetAttributes <ValidateAttr>(customMethodInfo.Method); //参数列表 customMethodInfo.ParamterInfos = customMethodInfo.Method.GetParameters(); //返回值类型 customMethodInfo.RetureType = customMethodInfo.Method.ReturnType; //方法最后的更新时间 customMethodInfo.LastUpdateTime = DateTime.Now; //方法的执行次数 customMethodInfo.Count = 1; #endregion #region 加了双重锁 防止死锁掉 将该方法加入缓存 if (!_idictMethod.Keys.Contains(key)) { lock (obj) { //防止在锁的时候 其他用户已经添加了键值 if (!_idictMethod.Keys.Contains(key)) { //将 此方法的信息记录到静态字典中 以便下次从内存中调用 _idictMethod.Add(key, customMethodInfo); } } } #endregion }
/// <summary> /// 得到方法缓存 /// </summary> /// <param name="key"></param> /// <returns></returns> internal static CustomMethodInfo GetMethodCache(string key) { CustomMethodInfo customMethodInfo = null; if (_idictMethod.Keys.Contains(key)) { customMethodInfo = _idictMethod[key]; #region 存在缓存中 更新次数和调用事件 //更新执行时间 customMethodInfo.LastUpdateTime = DateTime.Now; //访问次数加1 customMethodInfo.Count++; #endregion } return(customMethodInfo); }
/// <summary> /// 初始化方法 /// </summary> /// <exception cref="Ajax404Exception">没有找到页面</exception> public void InitMethod() { CustomMethodInfo customMethodInfo = MethodCache.GetMethodCache(this._methodPathInfo.ToString()); #region 获取该方法 if (customMethodInfo == null) { #region 如果缓存中不存在 将会反射重新获取方法信息 并且记录缓存 customMethodInfo = ReflectionHelper.GetMethodBaseInfo(this._methodPathInfo, MethodCache.BINDING_ATTR); if (customMethodInfo == null) { throw new Ajax404Exception(string.Format("没有找到页面{0}", this._methodPathInfo.MethodName)); } else { //添加进缓存 MethodCache.AddMethodCache(_methodPathInfo.ToString(), customMethodInfo); } #endregion } #endregion #region 判断该方法是否有 自定义网络访问的webMethodAttr的特性了 如果没有此特性 根本不给此方法 if (customMethodInfo.CurWebMethodAttr == null) { //没有该特性 throw new Ajax404Exception("没有找到此页面"); } else { //实例化网络请求方法的一些信息 用于传给特性使用 _httpRequestDescription = new HttpRequestDescription { Context = this._context, WebParameters = customMethodInfo.CurWebMethodAttr.GetWebParameters(this._context), CurrentMethodInfo = customMethodInfo }; } #endregion this.CurCustomMethodInfo = customMethodInfo; }
/// <summary> /// 添加一个方法的缓存 /// </summary> /// <param name="key">方法对应的键值</param> /// <param name="customMethodInfo">缓存中的实体信息</param> internal static void AddMethodCache(string key,CustomMethodInfo customMethodInfo) { #region 初始化方法的一些信息 //特性列表 customMethodInfo.AttrList = ReflectionHelper.GetAttributes<ValidateAttr>(customMethodInfo.Method); //参数列表 customMethodInfo.ParamterInfos = customMethodInfo.Method.GetParameters(); //返回值类型 customMethodInfo.RetureType = customMethodInfo.Method.ReturnType; //方法最后的更新时间 customMethodInfo.LastUpdateTime = DateTime.Now; //方法的执行次数 customMethodInfo.Count = 1; #endregion #region 加了双重锁 防止死锁掉 将该方法加入缓存 if (!_idictMethod.Keys.Contains(key)) { lock (obj) { //防止在锁的时候 其他用户已经添加了键值 if (!_idictMethod.Keys.Contains(key)) { //将 此方法的信息记录到静态字典中 以便下次从内存中调用 _idictMethod.Add(key, customMethodInfo); } } } #endregion }
/// <summary> /// 初始化缓存 /// </summary> private static void InitCache() { ICollection assemblies = BuildManager.GetReferencedAssemblies(); foreach (Assembly assembly in assemblies) { string assemblyName= assembly.GetName().Name; if (UrlConfig.ASSEMBLY.Equals(assemblyName)) { //如果在指定的ajax的业务的程序集中 //添加到程序集的缓存中 if (!_idictAssemby.Keys.Contains(assemblyName)) { _idictAssemby.Add(assemblyName, assembly); } try { foreach (Type t in assembly.GetExportedTypes()) { Type[] allInterface = t.GetInterfaces(); foreach (Type interfaceName in allInterface) { if ("IAjax".Equals(interfaceName.Name)) { // 该类有IAjax的接口 则默认添加进缓存 添加到类的缓存中 if (!_idictClass.Keys.Contains(t.FullName)) { _idictClass.Add(t.FullName, t); } } } } } catch { } } } //针对方法添加缓存 foreach (string className in _idictClass.Keys) { foreach (MethodInfo methodInfo in _idictClass[className].GetMethods(BINDING_ATTR)) { try { List<ValidateAttr> attrList = ReflectionHelper.GetAttributes<ValidateAttr>(methodInfo); //有标志的WebMethodAttr属性 添加进方法的缓存 WebMethodAttr webMethodAttr = attrList.Find(x => x is WebMethodAttr) as WebMethodAttr; if (webMethodAttr == null) { //没有该特性 continue; } CustomMethodInfo customMethonfInfo = new CustomMethodInfo() { AttrList = attrList, Count = 0, LastUpdateTime = DateTime.Now, Method = methodInfo, RetureType = methodInfo.ReturnType, ParamterInfos = methodInfo.GetParameters(), ClassType = _idictClass[className], Assembly = _idictClass[className].Assembly }; //添加进方法的缓存里面去 if (!_idictMethod.Keys.Contains(className + "." + methodInfo.Name)) { _idictMethod.Add(className + "." + methodInfo.Name, customMethonfInfo); } } catch { } } } }
/// <summary> /// 得到方法的最基本的信息 /// </summary> /// <returns></returns> private CustomMethodInfo GetMethodBaseInfo() { CustomMethodInfo customMethodInfo = new CustomMethodInfo(); try { //得到程序集 customMethodInfo.Assembly = Assembly.Load(this._methodPathInfo.Assembly); //得到类的类型 Type t = customMethodInfo.Assembly.GetType(this._methodPathInfo.Assembly+"."+this._methodPathInfo.ClassName,true,true); //得到类的实例 customMethodInfo.Instance = Activator.CreateInstance(t); //得到方法 customMethodInfo.Method = t.GetMethod(this._methodPathInfo.MethodName,_bindingAttr); if (customMethodInfo.Method == null) { //没有得到方法 把整个信息置空 customMethodInfo = null; } } catch { throw; } return customMethodInfo; }
/// <summary> /// 执行方法 /// </summary> /// <param name="customMethodInfo">方法的详细信息</param> /// <returns></returns> public object ExecMethod(CustomMethodInfo customMethodInfo) { object ret = null; try { ParameterHelper parameterHelper = new ParameterHelper(this._httpRequestDescription.WebParameters); //得到了方法中参数的值 object[] args = parameterHelper.GetParameterValues(customMethodInfo.ParamterInfos); //动态执行方法 ret = customMethodInfo.Method.Invoke(customMethodInfo.Instance, args); } catch { throw; } return ret; }
/// <summary> /// 初始化缓存 /// </summary> private static void InitCache() { ICollection assemblies = BuildManager.GetReferencedAssemblies(); foreach (Assembly assembly in assemblies) { string assemblyName = assembly.GetName().Name; if (UrlConfig.ASSEMBLY.Equals(assemblyName)) { //如果在指定的ajax的业务的程序集中 //添加到程序集的缓存中 if (!_idictAssemby.Keys.Contains(assemblyName)) { _idictAssemby.Add(assemblyName, assembly); } try { foreach (Type t in assembly.GetExportedTypes()) { Type[] allInterface = t.GetInterfaces(); foreach (Type interfaceName in allInterface) { if ("IAjax".Equals(interfaceName.Name)) { // 该类有IAjax的接口 则默认添加进缓存 添加到类的缓存中 if (!_idictClass.Keys.Contains(t.FullName)) { _idictClass.Add(t.FullName, t); } } } } } catch { } } } //针对方法添加缓存 foreach (string className in _idictClass.Keys) { foreach (MethodInfo methodInfo in _idictClass[className].GetMethods(BINDING_ATTR)) { try { List <ValidateAttr> attrList = ReflectionHelper.GetAttributes <ValidateAttr>(methodInfo); //有标志的WebMethodAttr属性 添加进方法的缓存 WebMethodAttr webMethodAttr = attrList.Find(x => x is WebMethodAttr) as WebMethodAttr; if (webMethodAttr == null) { //没有该特性 continue; } CustomMethodInfo customMethonfInfo = new CustomMethodInfo() { AttrList = attrList, Count = 0, LastUpdateTime = DateTime.Now, Method = methodInfo, RetureType = methodInfo.ReturnType, ParamterInfos = methodInfo.GetParameters(), ClassType = _idictClass[className], Assembly = _idictClass[className].Assembly }; //添加进方法的缓存里面去 if (!_idictMethod.Keys.Contains(className + "." + methodInfo.Name)) { _idictMethod.Add(className + "." + methodInfo.Name, customMethonfInfo); } } catch { } } } }
/// <summary> /// 初始化方法 /// </summary> /// <exception cref="Ajax404Exception">没有找到页面</exception> public void InitMethod() { CustomMethodInfo customMethodInfo = MethodCache.GetMethodCache(this._methodPathInfo.ToString()); #region 获取该方法 if (customMethodInfo == null) { #region 如果缓存中不存在 将会反射重新获取方法信息 并且记录缓存 customMethodInfo = ReflectionHelper.GetMethodBaseInfo(this._methodPathInfo, MethodCache.BINDING_ATTR); if (customMethodInfo == null) { throw new Ajax404Exception(string.Format("没有找到页面{0}", this._methodPathInfo.MethodName)); } else { //添加进缓存 MethodCache.AddMethodCache(_methodPathInfo.ToString(), customMethodInfo); } #endregion } #endregion #region 判断该方法是否有 自定义网络访问的webMethodAttr的特性了 如果没有此特性 根本不给此方法 if (customMethodInfo.CurWebMethodAttr == null) { //没有该特性 throw new Ajax404Exception("没有找到此页面"); } else { //实例化网络请求方法的一些信息 用于传给特性使用 _httpRequestDescription = new HttpRequestDescription { Context = this._context, WebParameters = customMethodInfo.CurWebMethodAttr.GetWebParameters(this._context), CurrentMethodInfo = customMethodInfo }; } #endregion this.CurCustomMethodInfo=customMethodInfo; }