示例#1
0
        private string InvokeMethod(string path, int argumentsCount,
                                    Func <string, WoopsaValueType, WoopsaValue> getArgumentByName)
        {
            IWoopsaElement item = FindByPath(path);

            if (item is IWoopsaMethod)
            {
                IWoopsaMethod method = item as IWoopsaMethod;
                if (argumentsCount == method.ArgumentInfos.Count())
                {
                    List <WoopsaValue> woopsaArguments = new List <WoopsaValue>();
                    foreach (var argInfo in method.ArgumentInfos)
                    {
                        WoopsaValue argumentValue = getArgumentByName(argInfo.Name, argInfo.Type);
                        if (argumentValue == null)
                        {
                            string message = String.Format("Missing argument {0} for method {1}", argInfo.Name, item.Name);
                            OnLog(WoopsaVerb.Invoke, path, NoArguments, message, false);
                            throw new WoopsaInvalidOperationException(message);
                        }
                        else
                        {
                            woopsaArguments.Add(argumentValue);
                        }
                    }
                    WoopsaValue[] argumentsArray = woopsaArguments.ToArray();
                    IWoopsaValue  methodResult   = method.Invoke(argumentsArray);
                    string        result         = methodResult != null?methodResult.Serialize() : WoopsaConst.WoopsaNull;

                    OnLog(WoopsaVerb.Invoke, path, argumentsArray, result, true);
                    return(result);
                }
                else
                {
                    string message = String.Format("Wrong argument count for method {0}", item.Name);
                    OnLog(WoopsaVerb.Invoke, path, NoArguments, message, false);
                    throw new WoopsaInvalidOperationException(message);
                }
            }
            else
            {
                string message = String.Format("Cannot invoke a {0}", item.GetType());
                OnLog(WoopsaVerb.Invoke, path, NoArguments, message, false);
                throw new WoopsaInvalidOperationException(message);
            }
        }
示例#2
0
        internal string InvokeMethod(string path, NameValueCollection arguments)
        {
            IWoopsaElement item = FindByPath(path);

            if (item is IWoopsaMethod)
            {
                int           argumentsCount = arguments != null ? arguments.Count : 0;
                IWoopsaMethod method         = item as IWoopsaMethod;
                if (argumentsCount == method.ArgumentInfos.Count())
                {
                    List <WoopsaValue> woopsaArguments = new List <WoopsaValue>();
                    foreach (var argInfo in method.ArgumentInfos)
                    {
                        string argumentValue = arguments[argInfo.Name];
                        if (argumentValue == null)
                        {
                            throw new WoopsaInvalidOperationException(String.Format(
                                                                          "Missing argument {0} for method {1}", argInfo.Name, item.Name));
                        }
                        woopsaArguments.Add(WoopsaValue.CreateChecked(argumentValue, argInfo.Type));
                    }
                    IWoopsaValue result = method.Invoke(woopsaArguments.ToArray());
                    return(result != null?result.Serialize() : WoopsaConst.WoopsaNull);
                }
                else
                {
                    throw new WoopsaInvalidOperationException(String.Format(
                                                                  "Wrong argument count for method {0}", item.Name));
                }
            }
            else
            {
                throw new WoopsaInvalidOperationException(String.Format(
                                                              "Cannot invoke a {0}", item.GetType()));
            }
        }
示例#3
0
        private static string SerializeMetadata(this IWoopsaMethod method)
        {
            string arguments = method.ArgumentInfos.SerializeMetadata();

            return(String.Format(MetadataMethod, JsonEscape(method.Name), method.ReturnType, arguments));
        }