private void Configure() { foreach (MethodInfo mi in this.Type.GetMethods()) { if (DirectMethod.IsMethod(mi)) { this.methods.Add(Utility.GetName(mi), new DirectMethod(mi)); } } }
internal object Execute(DirectRequest request) { DirectAction action = this.actions[request.Action]; if (action == null) { throw new DirectException("Unable to find action, " + request.Action); } DirectMethod method = action.GetMethod(request.Method); if (method == null) { throw new DirectException("Unable to find action, " + request.Method); } Type type = action.Type; if (request.Data == null) { if (method.Parameters > 0) { throw new DirectException("Parameters length does not match"); } } else { if (request.Data.Length > 1 && method.IsForm) { throw new DirectException("Form methods can only have a single parameter."); } else if (request.Data.Length != method.Parameters) { throw new DirectException("Parameters length does not match"); } } try { this.SanitizeDates(method.Method, request); object[] param = request.Data; if (method.ParseAsJson) { param = new object[] { method.GetParseData(request.RequestData) }; } return(method.Method.Invoke(type.Assembly.CreateInstance(type.FullName), param)); } catch (Exception ex) { throw new DirectException("Error occurred while calling Direct method: " + ex.Message); } }
internal object Execute(DirectRequest request) { DirectAction action = this.actions[request.Action]; if (action == null) { throw new DirectException("Unable to find action, " + request.Action); } DirectMethod method = action.GetMethod(request.Method); if (method == null) { throw new DirectException("Unable to find action, " + request.Method); } Type type = action.Type; if (request.Data == null) { if (method.Parameters > 0) { throw new DirectException("Parameters length does not match"); } } else { if (request.Data.Length > 1 && method.IsForm) { throw new DirectException("Form methods can only have a single parameter."); } else if (request.Data.Length != method.Parameters) { throw new DirectException("Parameters length does not match"); } } this.SanitizeDates(method.Method, request); object[] param = request.Data; if ((param != null) && (!request.IsUpload)) { // If any of the parameters of the method we are calling are classes // or have the JsonObject attribute then we need to deserialize them... var methodParams = method.Method.GetParameters(); Type parameterType; for (int i = 0; i < methodParams.Length; ++i) { // Is parameter a class or marked as a JsonObject? parameterType = methodParams[i].ParameterType; if ((parameterType != typeof(String)) && (parameterType.IsClass || Utility.HasAttribute(parameterType, typeof(JsonObjectAttribute)))) { // Allow class parameters to be null if (param[i] != null) { param[i] = JsonConvert.DeserializeObject(param[i].ToString(), parameterType); } } } } return(method.Method.Invoke(type.Assembly.CreateInstance(type.FullName), param)); }