using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; namespace WxOrder.Framework.Commons.Dialogs { /// /// This is a generic parameters base class used for Dialog Parameters and Navigation Parameters. /// public abstract class ParametersBase : IParameters, IEnumerable> { private readonly List> _entries = new List>(); /// /// Default constructor. /// protected ParametersBase() { } /// /// Constructs a list of parameters. /// /// Query string to be parsed. protected ParametersBase(string query) { if (!string.IsNullOrWhiteSpace(query)) { int num = query.Length; for (int i = ((query.Length > 0) && (query[0] == '?')) ? 1 : 0; i < num; i++) { int startIndex = i; int num4 = -1; while (i < num) { char ch = query[i]; if (ch == '=') { if (num4 < 0) { num4 = i; } } else if (ch == '&') { break; } i++; } string key = null; string value; if (num4 >= 0) { key = query.Substring(startIndex, num4 - startIndex); value = query.Substring(num4 + 1, (i - num4) - 1); } else { value = query.Substring(startIndex, i - startIndex); } if (key != null) Add(Uri.UnescapeDataString(key), Uri.UnescapeDataString(value)); } } } /// /// Searches Parameter collection and returns value if Collection contains key. /// Otherwise returns null. /// /// The key for the value to be returned. /// The value of the parameter referenced by the key; otherwise null. public object this[string key] { get { foreach (var entry in _entries) { if (string.Compare(entry.Key, key, StringComparison.Ordinal) == 0) { return entry.Value; } } return null; } } /// /// The count, or number, of parameters in collection. /// public int Count => _entries.Count; /// /// Returns an IEnumerable of the Keys in the collection. /// public IEnumerable Keys => _entries.Select(x => x.Key); /// /// Adds the key and value to the parameters collection. /// /// The key to reference this value in the parameters collection. /// The value of the parameter to store. public void Add(string key, object value) => _entries.Add(new KeyValuePair(key, value)); /// /// Checks collection for presence of key. /// /// The key to check in the collection. /// true if key exists; else returns false. public bool ContainsKey(string key) => _entries.ContainsKey(key); /// /// Gets an enumerator for the KeyValuePairs in parameter collection. /// /// Enumerator. public IEnumerator> GetEnumerator() => _entries.GetEnumerator(); /// /// Returns the value of the member referenced by key. /// /// The type of object to be returned. /// The key for the value to be returned. /// Returns a matching parameter of if one exists in the Collection. public T GetValue(string key) => _entries.GetValue(key); /// /// Returns an IEnumerable of all parameters. /// /// The type for the values to be returned. /// The key for the values to be returned. ///Returns a IEnumerable of all the instances of type . public IEnumerable GetValues(string key) => _entries.GetValues(key); /// /// Checks to see if the parameter collection contains the value. /// /// The type for the values to be returned. /// The key for the value to be returned. /// Value of the returned parameter if it exists. public bool TryGetValue(string key, out T value) => _entries.TryGetValue(key, out value); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); /// /// Converts parameter collection to a parameter string. /// /// A string representation of the parameters. public override string ToString() { var queryBuilder = new StringBuilder(); if (_entries.Count > 0) { queryBuilder.Append('?'); var first = true; foreach (var kvp in _entries) { if (!first) { queryBuilder.Append('&'); } else { first = false; } queryBuilder.Append(Uri.EscapeDataString(kvp.Key)); queryBuilder.Append('='); queryBuilder.Append(Uri.EscapeDataString(kvp.Value != null ? kvp.Value.ToString() : "")); } } return queryBuilder.ToString(); } /// /// Adds a collection of parameters to the local parameter list. /// /// An IEnumerable of KeyValuePairs to add to the current parameter list. [EditorBrowsable(EditorBrowsableState.Never)] public void FromParameters(IEnumerable> parameters) => _entries.AddRange(parameters); } }