Exemplo n.º 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);
            }
        }
Exemplo n.º 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()));
            }
        }
Exemplo n.º 3
0
        public static object ToBaseType(this IWoopsaValue value, Type targetType)
        {
            switch (value.Type)
            {
            case WoopsaValueType.Null:
                if (targetType == typeof(void) || !targetType.IsValueType)
                {
                    return(null);
                }
                else
                {
                    break;
                }

            case WoopsaValueType.Logical:
                if (targetType == typeof(bool))
                {
                    return(value.ToBool());
                }
                else
                {
                    break;
                }

            case WoopsaValueType.Integer:
                if (targetType == typeof(byte))
                {
                    return(value.ToByte());
                }
                else if (targetType == typeof(sbyte))
                {
                    return(value.ToSByte());
                }
                else if (targetType == typeof(Int16))
                {
                    return(value.ToInt16());
                }
                else if (targetType == typeof(UInt16))
                {
                    return(value.ToUInt16());
                }
                else if (targetType == typeof(Int32))
                {
                    return(value.ToInt32());
                }
                else if (targetType == typeof(UInt32))
                {
                    return(value.ToUInt32());
                }
                else if (targetType == typeof(Int64))
                {
                    return(value.ToInt64());
                }
                else if (targetType == typeof(UInt64))
                {
                    return(value.ToUInt64());
                }
                else if (targetType == typeof(float))
                {
                    return(value.ToFloat());
                }
                else if (targetType == typeof(double))
                {
                    return(value.ToDouble());
                }
                else
                {
                    break;
                }

            case WoopsaValueType.Real:
                if (targetType == typeof(float))
                {
                    return(value.ToFloat());
                }
                else if (targetType == typeof(double))
                {
                    return(value.ToDouble());
                }
                else
                {
                    break;
                }

            case WoopsaValueType.DateTime:
                if (targetType == typeof(DateTime))
                {
                    return(value.ToDateTime());
                }
                else
                {
                    break;
                }

            case WoopsaValueType.TimeSpan:
                if (targetType == typeof(TimeSpan))
                {
                    return(value.ToTimeSpan());
                }
                else
                {
                    break;
                }

            case WoopsaValueType.Text:
            case WoopsaValueType.WoopsaLink:
            case WoopsaValueType.JsonData:
            case WoopsaValueType.ResourceUrl:
                if (targetType == typeof(string))
                {
                    return(value.AsText);
                }
                else
                {
                    break;
                }

            default:
                break;
            }
            throw new InvalidCastException(
                      string.Format("Unable to cast WoopsaValue '{0}' to type '{1}'",
                                    value.Serialize(), targetType.FullName));
        }