コード例 #1
0
 public Task <TResult> SendCommandAsync <TResult>(ServerQueryCommand <TResult> command) where TResult : ServerQueryBaseResult
 {
     return(SendCommandAsync(client => client.Send(command.ToCommandString()), command.Parser));
 }
コード例 #2
0
        public static string ToCommandString <T>(this ServerQueryCommand <T> command) where T : ServerQueryBaseResult
        {
            StringBuilder commandBuilder = new StringBuilder();

            // Command
            commandBuilder.Append(command.Command);

            // Parameters
            // key=value
            // key=value[0]|key=value[1]|key=value[2] if value is a collection
            // String values must be escaped
            foreach (KeyValuePair <Parameter, object> parameter in command.Parameters)
            {
                // Don't add parameters with null values
                if (parameter.Value == null)
                {
                    continue;
                }

                // Don't add parameters with string.empty values
                if (parameter.Value is string && string.IsNullOrWhiteSpace((string)parameter.Value))
                {
                    continue;
                }

                // Check if we've been passed a collection of values for this parameter
                IList values = parameter.Value as IList;

                // key=value[0]|key=value[1]|key=value[2] if value is a collection
                if (values != null && values.Count > 0)
                {
                    commandBuilder.Append(" ");

                    for (int i = 0; i < values.Count; i++)
                    {
                        if (i > 0)
                        {
                            commandBuilder.Append("|");
                        }

                        commandBuilder.Append(parameter.Key);
                        commandBuilder.Append("=");
                        commandBuilder.Append(values[i].ToString().Escape());
                    }

                    continue;
                }

                // Not a collection, just a single key/value pair
                // key=value
                commandBuilder.Append(" ");
                commandBuilder.Append(parameter.Key);
                commandBuilder.Append("=");
                commandBuilder.Append(parameter.Value.ToString().Escape());
            }

            // Options
            // -option
            foreach (Option option in command.Options)
            {
                commandBuilder.Append(" ");
                commandBuilder.Append("-");
                commandBuilder.Append(option);
            }

            return(commandBuilder.ToString());
        }