private bool TryUrlQueryParse(string query, out HandlerData handlerData) { handlerData = new HandlerData(); handlerData.Params = new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase); var keyValues = (query ?? "").Split('&'); foreach (var keyValue in keyValues) { var paris = keyValue.Split('='); if (paris.Length != 2) { continue; } string name = paris[0]; string value = paris[1]; if (string.IsNullOrEmpty(name)) { continue; } if (string.Compare("Handler", name, StringComparison.OrdinalIgnoreCase) == 0) { handlerData.Name = value; continue; } handlerData.Params[name] = value; } return(!string.IsNullOrEmpty(handlerData.Name)); }
private static object ParseObject(HandlerData handlerData, Type paramType) { var paramValues = handlerData.Params; var obj = paramType.CreateInstance(); foreach (PropertyInfo prop in paramType.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)) { if (paramValues.ContainsKey(prop.Name) && (prop.PropertyType.IsValueType || typeof(string) == prop.PropertyType)) { var value = Convert.ChangeType(paramValues[prop.Name], prop.PropertyType); prop.SetValue(obj, value, null); } } return(obj); }
/// <summary> /// /// </summary> /// <param name="handlerData"></param> /// <returns></returns> public static ResponseData Excute(HandlerData handlerData) { Type type; if (!handlerTypes.TryGetValue(handlerData.Name, out type)) { throw new HandlerException(StateCode.NoHandler, "Not Found Handler"); } dynamic instance = type.CreateInstance(); Type paramType = type.GetInterfaces().First().GetGenericArguments().First(); dynamic paramObj = ParseObject(handlerData, paramType); var result = instance.Excute(paramObj); return(result as ResponseData); }
/// <summary> /// /// </summary> /// <param name="query"></param> /// <param name="handlerData"></param> /// <returns></returns> protected bool TryUrlQueryParse(string query, out HandlerData handlerData) { handlerData = new HandlerData(); handlerData.Params = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase); var keyValues = (query ?? "").Split('&'); foreach (var keyValue in keyValues) { var paris = keyValue.Split('='); if (paris.Length != 2) continue; string name = paris[0]; string value = HttpUtility.UrlDecode(paris[1]); if (string.IsNullOrEmpty(name)) continue; if (string.Compare("Handler", name, StringComparison.OrdinalIgnoreCase) == 0) { handlerData.Name = value; continue; } handlerData.Params[name] = value; } return !string.IsNullOrEmpty(handlerData.Name); }
private static object ParseObject(HandlerData handlerData, Type paramType) { var paramValues = handlerData.Params; var obj = paramType.CreateInstance(); foreach (PropertyInfo prop in paramType.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)) { if (paramValues.ContainsKey(prop.Name) && (prop.PropertyType.IsValueType || typeof(string) == prop.PropertyType)) { object value; var val = paramValues[prop.Name]; if (prop.PropertyType == typeof(bool)) { value = val.ToBool(); } else { value = Convert.ChangeType(val, prop.PropertyType); } prop.SetValue(obj, value, null); } } return obj; }
/// <summary> /// /// </summary> /// <param name="handlerData"></param> /// <returns></returns> public static ResponseData Excute(HandlerData handlerData) { Type type; if (!handlerTypes.TryGetValue(handlerData.Name, out type)) { throw new HandlerException(StateCode.NoHandler, "Not Found Handler"); } dynamic instance = type.CreateInstance(); Type paramType = type.GetInterfaces().First().GetGenericArguments().First(); dynamic paramObj = ParseObject(handlerData, paramType); var result = instance.Excute(paramObj); return result as ResponseData; }