BaseExtensions.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Threading;
  9. namespace MediaBrowser.Common.Extensions
  10. {
  11. /// <summary>
  12. /// Class BaseExtensions
  13. /// </summary>
  14. public static class BaseExtensions
  15. {
  16. /// <summary>
  17. /// Tries the add.
  18. /// </summary>
  19. /// <typeparam name="TKey">The type of the T key.</typeparam>
  20. /// <typeparam name="TValue">The type of the T value.</typeparam>
  21. /// <param name="dictionary">The dictionary.</param>
  22. /// <param name="key">The key.</param>
  23. /// <param name="value">The value.</param>
  24. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  25. public static bool TryAdd<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value)
  26. {
  27. if (dictionary.ContainsKey(key))
  28. {
  29. return false;
  30. }
  31. dictionary.Add(key, value);
  32. return true;
  33. }
  34. /// <summary>
  35. /// Provides an additional overload for string.split
  36. /// </summary>
  37. /// <param name="val">The val.</param>
  38. /// <param name="separator">The separator.</param>
  39. /// <param name="options">The options.</param>
  40. /// <returns>System.String[][].</returns>
  41. public static string[] Split(this string val, char separator, StringSplitOptions options)
  42. {
  43. return val.Split(new[] { separator }, options);
  44. }
  45. /// <summary>
  46. /// Invokes an action after a specified delay
  47. /// </summary>
  48. /// <param name="dispatcher">The dispatcher.</param>
  49. /// <param name="action">The action.</param>
  50. /// <param name="delayMs">The delay ms.</param>
  51. public static void InvokeWithDelay(this Dispatcher dispatcher, Action action, long delayMs)
  52. {
  53. var timer = new DispatcherTimer(DispatcherPriority.Normal, dispatcher);
  54. timer.Interval = TimeSpan.FromMilliseconds(delayMs);
  55. timer.Tick += (sender, args) =>
  56. {
  57. timer.Stop();
  58. action();
  59. };
  60. timer.Start();
  61. }
  62. /// <summary>
  63. /// Provides a non-blocking method to start a process and wait asynchronously for it to exit
  64. /// </summary>
  65. /// <param name="process">The process.</param>
  66. /// <returns>Task{System.Boolean}.</returns>
  67. public static Task<bool> RunAsync(this Process process)
  68. {
  69. var tcs = new TaskCompletionSource<bool>();
  70. process.EnableRaisingEvents = true;
  71. process.Exited += (sender, args) => tcs.SetResult(true);
  72. process.Start();
  73. return tcs.Task;
  74. }
  75. /// <summary>
  76. /// Shuffles an IEnumerable
  77. /// </summary>
  78. /// <typeparam name="T"></typeparam>
  79. /// <param name="list">The list.</param>
  80. /// <returns>IEnumerable{``0}.</returns>
  81. public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> list)
  82. {
  83. return list.OrderBy(x => Guid.NewGuid());
  84. }
  85. /// <summary>
  86. /// Gets the M d5.
  87. /// </summary>
  88. /// <param name="str">The STR.</param>
  89. /// <returns>Guid.</returns>
  90. public static Guid GetMD5(this string str)
  91. {
  92. using (var provider = new MD5CryptoServiceProvider())
  93. {
  94. return new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(str)));
  95. }
  96. }
  97. /// <summary>
  98. /// Gets the MB id.
  99. /// </summary>
  100. /// <param name="str">The STR.</param>
  101. /// <param name="aType">A type.</param>
  102. /// <returns>Guid.</returns>
  103. /// <exception cref="System.ArgumentNullException">aType</exception>
  104. public static Guid GetMBId(this string str, Type aType)
  105. {
  106. if (aType == null)
  107. {
  108. throw new ArgumentNullException("aType");
  109. }
  110. return (aType.FullName + str.ToLower()).GetMD5();
  111. }
  112. /// <summary>
  113. /// Helper method for Dictionaries since they throw on not-found keys
  114. /// </summary>
  115. /// <typeparam name="T"></typeparam>
  116. /// <typeparam name="U"></typeparam>
  117. /// <param name="dictionary">The dictionary.</param>
  118. /// <param name="key">The key.</param>
  119. /// <param name="defaultValue">The default value.</param>
  120. /// <returns>``1.</returns>
  121. public static U GetValueOrDefault<T, U>(this Dictionary<T, U> dictionary, T key, U defaultValue)
  122. {
  123. U val;
  124. if (!dictionary.TryGetValue(key, out val))
  125. {
  126. val = defaultValue;
  127. }
  128. return val;
  129. }
  130. /// <summary>
  131. /// Gets the attribute value.
  132. /// </summary>
  133. /// <param name="str">The STR.</param>
  134. /// <param name="attrib">The attrib.</param>
  135. /// <returns>System.String.</returns>
  136. /// <exception cref="System.ArgumentNullException">attrib</exception>
  137. public static string GetAttributeValue(this string str, string attrib)
  138. {
  139. if (attrib == null)
  140. {
  141. throw new ArgumentNullException("attrib");
  142. }
  143. string srch = "[" + attrib + "=";
  144. int start = str.IndexOf(srch, StringComparison.OrdinalIgnoreCase);
  145. if (start > -1)
  146. {
  147. start += srch.Length;
  148. int end = str.IndexOf(']', start);
  149. return str.Substring(start, end - start);
  150. }
  151. return null;
  152. }
  153. }
  154. }