public static void RegistController(Type t) { if (!t.GetInterfaces().Contains(typeof(IMyController))) { return; } //dynamic controller = Activator.CreateInstance(t); //自动注册该类的所有方法 string controllername = t.Name.ToLower(); ControllerDescription cd = new ControllerDescription(t); if (s_ServiceCommandDict.ContainsKey(controllername)) { return; } s_ServiceCommandDict.Add(controllername, cd); foreach (MethodInfo m in cd.ControllerType.GetMethods(ActionBindingFlags)) { ActionAttribute actionAttr = m.GetMyAttribute <ActionAttribute>(); if (actionAttr != null) { ActionDescription actionDescription = new ActionDescription(m, actionAttr) { PageController = cd }; string actionname = m.Name.ToLower(); s_PageActionDict.Add(controllername + "/" + actionname, actionDescription); } } }
private static object[] GetActionCallParameters(TrafficContext context, ActionDescription action) { if (action.Parameters == null || action.Parameters.Length == 0) { return(null); } object[] parameters = new object[action.Parameters.Length]; for (int i = 0; i < action.Parameters.Length; i++) { ParameterInfo p = action.Parameters[i]; if (p.IsOut) { continue; } if (p.ParameterType == typeof(VoidType)) { continue; } Type paramterType = p.ParameterType.GetRealType(); object val = context.Request.GetValue(i, paramterType); if (val != null) { parameters[i] = val; } else { if (p.ParameterType.IsValueType && p.ParameterType.IsNullableType() == false) { throw new ArgumentException("未能找到指定的参数值:" + p.Name); } } } return(parameters); }
///// <summary> ///// 根据要调用的方法名返回对应的 Action ///// </summary> ///// <param name="controller"></param> ///// <param name="action"></param> ///// <param name="request"></param> ///// <returns></returns> //private static ActionDescription GetServiceAction(Type controller, string action, HttpRequest request) //{ // if (controller == null) // throw new ArgumentNullException("controller"); // if (string.IsNullOrEmpty(action)) // throw new ArgumentNullException("action"); // // 首先尝试从缓存中读取 // string key = request.HttpMethod + "#" + controller.FullName + "@" + action; // ActionDescription mi = (ActionDescription)s_ServiceActionTable[key]; // if (mi == null) // { // bool saveToCache = true; // MethodInfo method = FindAction(action, controller, request); // if (method == null) // { // // 如果Action的名字是submit并且是POST提交,则需要自动寻找Action // // 例如:多个提交都采用一样的方式:POST /Ajax/Product/submit // if (action.IsSame("submit") && request.HttpMethod.IsSame("POST")) // { // // 自动寻找Action // method = FindSubmitAction(controller, request); // saveToCache = false; // } // } // if (method == null) // return null; // var attr = method.GetMyAttribute<ActionAttribute>(); // mi = new ActionDescription(method, attr); // if (saveToCache) // s_ServiceActionTable[key] = mi; // } // return mi; //} //private static MethodInfo FindAction(string action, Type controller, HttpRequest request) //{ // foreach (MethodInfo method in controller.GetMethods()) // { // if (method.Name.IsSame(action)) // { // if (MethodActionIsMatch(method, request)) // return method; // } // } // return null; //} //private static MethodInfo FindSubmitAction(Type controller, HttpRequest request) //{ // string[] keys = request.Form.AllKeys; // foreach (MethodInfo method in controller.GetMethods()) // { // string key = keys.FirstOrDefault(x => method.Name.IsSame(x)); // if (key != null && MethodActionIsMatch(method, request)) // return method; // } // return null; //} //private static bool MethodActionIsMatch(MethodInfo method, HttpRequest request) //{ // var attr = method.GetMyAttribute<ActionAttribute>(); // if (attr != null) // { // if (attr.AllowExecute(request.HttpMethod)) // return true; // } // return false; //} /// <summary> /// 根据一个页面请求路径,返回内部表示的调用信息。 /// </summary> /// <param name="url"></param> /// <returns></returns> public static InvokeInfo GetActionInvokeInfo(string commandid) { if (commandid == "") { throw new ArgumentNullException("url"); } ActionDescription action = null; if (s_PageActionDict.TryGetValue(commandid, out action) == false) { return(null); } InvokeInfo vkInfo = new InvokeInfo(); vkInfo.Controller = action.PageController; vkInfo.Action = action; if (vkInfo.Action.MethodInfo.IsStatic == false) { vkInfo.Instance = Activator.CreateInstance(vkInfo.Controller.ControllerType); } return(vkInfo); }