2
0

BaseExtensions.cs 5.0 KB

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