1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- namespace EventBus
- {
- public sealed class Properties
- {
- internal Dictionary<string, string> Filter { get; set; } = new Dictionary<string, string>();
- public void Add(string name, string value)
- {
- if (Filter.TryGetValue(name, out string val)) Filter[name] = value;
- else Filter.Add(name, value);
- }
- public string this[string name]
- {
- get
- {
- if (Filter.ContainsKey(name)) return Filter[name];
- else return null;
- }
- set
- {
- if (!Filter.ContainsKey(name)) Filter.Add(name, value);
- else Filter[name] = value;
- }
- }
- public static Properties GetProperties(string prostr)
- {
- if (string.IsNullOrEmpty(prostr)) return Properties.Default;
- Properties properties = new Properties();
- try
- {
- foreach (var item in prostr.Split("\r\n"))
- {
- var v = item.Split(":");
- properties[v[0]] = v[1];
- }
- }
- catch
- {
- }
- return properties;
- }
- public bool TryGetValue(string name, out string value) => Filter.TryGetValue(name, out value);
- public int Count => Filter.Count;
- public void Remove(string name) => Filter.Remove(name);
- public void Clear() => Filter.Clear();
- public IEnumerable<string> Values => Filter.Values;
- public IEnumerable<string> Names => Filter.Keys;
- /// <summary>
- /// 比较两个属性是否相等
- /// </summary>
- /// <param name="properties"></param>
- /// <returns></returns>
- internal bool Contains(Properties properties)
- {
- if (properties == null) return false;
- if (properties.Count != Count) return false;
- foreach (var name in Names)
- {
- if (!Filter.TryGetValue(name, out string val)) return false;
- else if (!val.Contains(properties[name])) return false;
- }
- return true;
- }
- public override string ToString()
- {
- return string.Join("\r\n", Filter.Select(x => $"{x.Key}:{x.Value}"));
- }
- public static Properties Default => new Properties();
- }
- }
|