BaseExtensions.cs 4.4 KB

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