Ejemplo n.º 1
0
        public MidgeCommandInvoker Create(ClientMessage clientMessage)
        {
            MidgeControllerInfo controllerInfo = ControllerParser.GetController(clientMessage.Controller);

            MidgeCommandInfo commandInfo = controllerInfo.GetCommandByName(clientMessage.Command);

            if (commandInfo == null)
            {
                throw new MidgeException("Command undefined");
            }

            List <object> args = new List <object>();

            foreach (var param in commandInfo.Params.OrderBy(i => i.Order))
            {
                var userParam = clientMessage.Parameters.FirstOrDefault(i =>
                                                                        i.Name.Equals(param.Name, StringComparison.OrdinalIgnoreCase));

                if (userParam == null && param.IsRequired)
                {
                    throw new MidgeException($"Parameter not found. Param name: {param.Name}");
                }

                Type actualParamType = param.ParamType;

                if (userParam == null)
                {
                    args.Add(null);
                    continue;
                }

                if (param.ParamType.IsGenericType && param.ParamType.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    actualParamType = param.ParamType.GetGenericArguments()[0];
                }


                object argument;



                try
                {
                    argument = userParam.Value.ToObject(actualParamType);

                    if (actualParamType.IsEnum)
                    {
                        if (!Enum.IsDefined(actualParamType, argument))
                        {
                            throw new Exception("Invalid value");
                        }

                        argument = Enum.ToObject(actualParamType, (int)argument);
                    }
                }
                catch
                {
                    throw new MidgeException($"Parameter {param.Name} has invalid type. Expected type is {actualParamType.Name}");
                }

                args.Add(argument);
            }


            return(new MidgeCommandInvoker(controllerInfo, commandInfo, args.ToArray()));
        }
Ejemplo n.º 2
0
 public MidgeCommandInvoker(MidgeControllerInfo controller, MidgeCommandInfo command, object[] args)
 {
     Controller = controller;
     Command    = command;
     _args      = args;
 }