Properties.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. namespace EventBus
  2. {
  3. public sealed class Properties
  4. {
  5. internal Dictionary<string, string> Filter { get; set; } = new Dictionary<string, string>();
  6. public void Add(string name, string value)
  7. {
  8. if (Filter.TryGetValue(name, out string val)) Filter[name] = value;
  9. else Filter.Add(name, value);
  10. }
  11. public string this[string name]
  12. {
  13. get
  14. {
  15. if (Filter.ContainsKey(name)) return Filter[name];
  16. else return null;
  17. }
  18. set
  19. {
  20. if (!Filter.ContainsKey(name)) Filter.Add(name, value);
  21. else Filter[name] = value;
  22. }
  23. }
  24. public bool TryGetValue(string name, out string value) => Filter.TryGetValue(name, out value);
  25. public int Count => Filter.Count;
  26. public void Remove(string name) => Filter.Remove(name);
  27. public void Clear() => Filter.Clear();
  28. public IEnumerable<string> Values => Filter.Values;
  29. public IEnumerable<string> Names => Filter.Keys;
  30. /// <summary>
  31. /// 比较两个属性是否相等
  32. /// </summary>
  33. /// <param name="properties"></param>
  34. /// <returns></returns>
  35. internal bool Contains(Properties properties)
  36. {
  37. if (properties == null) return false;
  38. if (properties.Count != Count) return false;
  39. foreach (var name in Names)
  40. {
  41. if (!Filter.TryGetValue(name, out string val)) return false;
  42. else if (!val.Contains(properties[name])) return false;
  43. }
  44. return true;
  45. }
  46. public static Properties Default => new Properties();
  47. }
  48. }