Exemple #1
0
        public static void ExecuteMethod(ActionHandler AH, ActionInputParams p, NodeGingerAction GA)
        {
            try
            {
                ParameterInfo[] PIs = AH.MethodInfo.GetParameters();

                object[] parameters = new object[PIs.Count()];

                int paramnum = 0;
                foreach (ParameterInfo PI in PIs)
                {
                    if (paramnum == 0)
                    {
                        // verify param 0 is GA
                        parameters[0] = GA;
                    }
                    else
                    {
                        object ActionParam = p[PI.Name];
                        if (ActionParam != null)
                        {
                            object val = null;
                            // For each type we need to get the val correctly so the function will get it right

                            if (PI.ParameterType == typeof(string))
                            {
                                val = p[PI.Name].Value;
                            }
                            else if (PI.ParameterType.IsEnum)
                            {
                                if (p[PI.Name].Value != null)
                                {
                                    val = Enum.Parse(PI.ParameterType, p[PI.Name].Value.ToString());
                                }
                                else
                                {
                                    // TODO: err or check if it is nullable enum
                                }
                            }
                            else if (PI.ParameterType == typeof(Int32))
                            {
                                val = p[PI.Name].GetValueAsInt();
                            }
                            else if (PI.ParameterType.IsGenericType && PI.ParameterType.GetGenericTypeDefinition() == typeof(List <>))
                            {
                                // This is List of objects
                                Type itemType = PI.ParameterType.GetGenericArguments()[0]; // List item type
                                Type listType = typeof(List <>).MakeGenericType(itemType); // List with the item type
                                // val = Activator.CreateInstance(listType);
                                val = JSONHelper.DeserializeObject(p[PI.Name].Value.ToString(), listType);
                            }
                            else
                            {
                                val = p[PI.Name].Value;
                            }

                            parameters[paramnum] = val;
                        }
                        else
                        {
                            //check if param is optional then ignore
                            if (!PI.HasDefaultValue)
                            {
                                throw new Exception("GingerAction is Missing Param/Value for ActionParam - " + PI.Name);
                            }
                            else
                            {
                                // from here on all params are optional...
                            }
                        }
                    }
                    paramnum++;
                }
                AH.MethodInfo.Invoke(AH.Instance, parameters);   // here is where we call the action directly with the relevant parameters
            }
            catch (Exception ex)
            {
                string message = ex.Message;
                if (ex.InnerException != null)
                {
                    message += Environment.NewLine + ex.InnerException.Message;
                }
                GA.AddError("Error when trying to invoke: " + AH.ServiceActionId + " - " + message);
            }
        }
Exemple #2
0
        public static void RunServiceAction(ActionHandler AH, ActionInputParams p, NodeGingerAction GA)
        {
            try
            {
                ParameterInfo[] PIs = AH.MethodInfo.GetParameters();

                object[] parameters = new object[PIs.Count()];

                int paramnum = 0;
                foreach (ParameterInfo PI in PIs)
                {
                    if (paramnum == 0)
                    {
                        // verify param 0 is GA
                        parameters[0] = GA;
                    }
                    else
                    {
                        object ActionParam = p[PI.Name];
                        if (ActionParam != null)
                        {
                            object val = null;
                            // For each type we need to get the val correctly so the function will get it right
                            if (PI.ParameterType.IsEnum)
                            {
                                if (p[PI.Name].Value != null)
                                {
                                    val = Enum.Parse(PI.ParameterType, p[PI.Name].Value.ToString());
                                }
                                else
                                {
                                    // TODO: err or check if it is nullable enum
                                }
                            }
                            else if (PI.ParameterType == typeof(Int32))
                            {
                                val = p[PI.Name].GetValueAsInt();
                            }
                            //TODO: handle all types
                            else
                            {
                                val = p[PI.Name].Value;
                            }

                            parameters[paramnum] = val;
                        }
                        else
                        {
                            //check if param is optional then ignore
                            if (!PI.HasDefaultValue)
                            {
                                throw new Exception("GingerAction is Missing Param/Value for ActionParam - " + PI.Name);
                            }
                            else
                            {
                                // from here on all params are optional...
                            }
                        }
                    }
                    paramnum++;
                }
                AH.MethodInfo.Invoke(AH.Instance, parameters);   // here is where we call the action directly with the relevant parameters
            }
            catch (Exception ex)
            {
                GA.AddError("Error when trying to invoke: " + AH.ServiceActionId + " - " + ex.Message);
            }
        }