Hapikit.Templates.Result.AppendDictionary C# (CSharp) Méthode

AppendDictionary() public méthode

public AppendDictionary ( OperatorInfo op, bool explode, string>.IDictionary dictionary ) : void
op OperatorInfo
explode bool
dictionary string>.IDictionary
Résultat void
        public void AppendDictionary(OperatorInfo op, bool explode, IDictionary<string, string> dictionary)
        {
            foreach (string key in dictionary.Keys)
            {
                _Result.Append(Encode(key, op.AllowReserved));
                if (explode) _Result.Append('='); else _Result.Append(',');
                AppendValue(dictionary[key], 0, op.AllowReserved);

                if (explode)
                {
                    _Result.Append(op.Seperator);
                }
                else
                {
                    _Result.Append(',');
                }
            }
            if (dictionary.Count() > 0)
            {
                _Result.Remove(_Result.Length - 1, 1);
            }
        }

Usage Example

Exemple #1
0
        private bool ProcessVariable(VarSpec varSpec, bool multiVariableExpression = false)
        {
            var varname = varSpec.VarName.ToString();

            if (_ParameterNames != null)
            {
                _ParameterNames.Add(varname);
            }

            if (!_Parameters.ContainsKey(varname) ||
                _Parameters[varname] == null ||
                (_Parameters[varname] is IList && ((IList)_Parameters[varname]).Count == 0) ||
                (_Parameters[varname] is IDictionary && ((IDictionary)_Parameters[varname]).Count == 0))
            {
                if (_resolvePartially == true)
                {
                    if (multiVariableExpression)
                    {
                        if (varSpec.First)
                        {
                            _Result.Append("{");
                        }

                        _Result.Append(varSpec.ToString());
                    }
                    else
                    {
                        _Result.Append("{");
                        _Result.Append(varSpec.ToString());
                        _Result.Append("}");
                    }
                    return(false);
                }
                return(false);
            }

            if (varSpec.First)
            {
                _Result.Append(varSpec.OperatorInfo.First);
            }
            else
            {
                _Result.Append(varSpec.OperatorInfo.Seperator);
            }

            object value = _Parameters[varname];

            // Handle Strings
            if (value is string)
            {
                var stringValue = (string)value;
                if (varSpec.OperatorInfo.Named)
                {
                    _Result.AppendName(varname, varSpec.OperatorInfo, string.IsNullOrEmpty(stringValue));
                }
                _Result.AppendValue(stringValue, varSpec.PrefixLength, varSpec.OperatorInfo.AllowReserved);
            }
            else
            {
                // Handle Lists
                var list = value as IList;
                if (list == null && value is IEnumerable <string> )
                {
                    list = ((IEnumerable <string>)value).ToList <string>();
                }
                ;
                if (list != null)
                {
                    if (varSpec.OperatorInfo.Named && !varSpec.Explode)      // exploding will prefix with list name
                    {
                        _Result.AppendName(varname, varSpec.OperatorInfo, list.Count == 0);
                    }

                    _Result.AppendList(varSpec.OperatorInfo, varSpec.Explode, varname, list);
                }
                else
                {
                    // Handle associative arrays
                    var dictionary = value as IDictionary <string, string>;
                    if (dictionary != null)
                    {
                        if (varSpec.OperatorInfo.Named && !varSpec.Explode)      // exploding will prefix with list name
                        {
                            _Result.AppendName(varname, varSpec.OperatorInfo, dictionary.Count() == 0);
                        }
                        _Result.AppendDictionary(varSpec.OperatorInfo, varSpec.Explode, dictionary);
                    }
                    else
                    {
                        // If above all fails, convert the object to string using the default object.ToString() implementation
                        var stringValue = value.ToString();
                        if (varSpec.OperatorInfo.Named)
                        {
                            _Result.AppendName(varname, varSpec.OperatorInfo, string.IsNullOrEmpty(stringValue));
                        }
                        _Result.AppendValue(stringValue, varSpec.PrefixLength, varSpec.OperatorInfo.AllowReserved);
                    }
                }
            }
            return(true);
        }