You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.5 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace BPA.UIControl.Models
  6. {
  7. /// <summary>
  8. /// 传输参数
  9. /// </summary>
  10. public class Parameters : IParameters, IEnumerable<KeyValuePair<string, object>>, IEnumerable
  11. {
  12. private readonly List<KeyValuePair<string, object>> entries = new List<KeyValuePair<string, object>>();
  13. /// <inheritdoc/>
  14. public object this[string key]
  15. {
  16. get
  17. {
  18. foreach (KeyValuePair<string, object> entry in entries)
  19. {
  20. if (string.Compare(entry.Key, key, StringComparison.Ordinal) == 0)
  21. {
  22. return entry.Value;
  23. }
  24. }
  25. return null;
  26. }
  27. }
  28. /// <inheritdoc/>
  29. public int Count => entries.Count;
  30. /// <inheritdoc/>
  31. public IEnumerable<string> Keys => entries.Select((x) => x.Key);
  32. /// <inheritdoc/>
  33. public void Add(string key, object value) => entries.Add(new KeyValuePair<string, object>(key, value));
  34. /// <inheritdoc/>
  35. public bool ContainsKey(string key) => Keys.Contains(key);
  36. /// <inheritdoc/>
  37. public IEnumerator<KeyValuePair<string, object>> GetEnumerator() => entries.GetEnumerator();
  38. /// <inheritdoc/>
  39. public T GetValue<T>(string key)
  40. {
  41. foreach (KeyValuePair<string, object> parameter in entries)
  42. {
  43. if (string.Compare(parameter.Key, key, StringComparison.Ordinal) == 0)
  44. {
  45. if (parameter.Value is T value)
  46. {
  47. return value;
  48. }
  49. throw new InvalidCastException("Unable to convert the value. ");
  50. }
  51. }
  52. return default;
  53. }
  54. /// <inheritdoc/>
  55. public bool TryGetValue<T>(string key, out T value)
  56. {
  57. foreach (KeyValuePair<string, object> parameter in entries)
  58. {
  59. if (string.Compare(parameter.Key, key, StringComparison.Ordinal) == 0)
  60. {
  61. if (parameter.Value is T result)
  62. {
  63. value = result;
  64. return true;
  65. }
  66. }
  67. }
  68. value = default;
  69. return false;
  70. }
  71. /// <inheritdoc/>
  72. IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
  73. }
  74. }