Exemplo n.º 1
0
 //Generic configuration method for a list of types.
 private void Configure(IEnumerable <Type> types)
 {
     foreach (Type type in types)
     {
         if (DirectAction.IsAction(type))
         {
             this.actions.Add(Utility.GetName(type), new DirectAction(type));
         }
     }
     this.Configured = true;
     this.ConstructApi();
 }
Exemplo n.º 2
0
        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);
            }
        }
Exemplo n.º 3
0
        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));
        }