Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

132 rader
5.0 KiB

  1. /* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. using System;
  22. using System.Collections.Generic;
  23. using System.ComponentModel;
  24. using System.Diagnostics.CodeAnalysis;
  25. using System.Globalization;
  26. using System.Linq;
  27. using System.Reflection;
  28. namespace Alphaleonis
  29. {
  30. internal static class Utils
  31. {
  32. #region EnumMemberToList
  33. public static IEnumerable<T> EnumMemberToList<T>()
  34. {
  35. Type enumType = typeof(T);
  36. // Can't use generic type constraints on value types, so have to do check like this.
  37. if (enumType.BaseType != typeof(Enum))
  38. throw new ArgumentException("T must be of type System.Enum");
  39. //Array enumValArray = Enum.GetValues(enumType);
  40. //List<T> enumValList = new List<T>(enumValArray.Length);
  41. IOrderedEnumerable<T> enumValArray = Enum.GetValues(enumType).Cast<T>().OrderBy(e => e.ToString());
  42. var enumValList = new List<T>(enumValArray.Count());
  43. enumValList.AddRange(enumValArray.Select(val => (T)Enum.Parse(enumType, val.ToString())));
  44. return enumValList;
  45. }
  46. #endregion // EnumMemberToList
  47. #region GetEnumDescription
  48. /// <summary>Gets an attribute on an enum field value.</summary>
  49. /// <returns>The description belonging to the enum option, as a string</returns>
  50. /// <param name="enumValue">One of the <see cref="Alphaleonis.Win32.Filesystem.DeviceGuid"/> enum types.</param>
  51. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  52. public static string GetEnumDescription(Enum enumValue)
  53. {
  54. FieldInfo fi = enumValue.GetType().GetField(enumValue.ToString());
  55. var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
  56. return attributes.Length > 0 ? attributes[0].Description : enumValue.ToString();
  57. }
  58. #endregion // GetEnumDescription
  59. #region IsNullOrWhiteSpace
  60. /// <summary>Indicates whether a specified string is null, empty, or consists only of white-space characters.</summary>
  61. /// <returns><see langword="true"/> if the <paramref name="value"/> parameter is null or <see cref="string.Empty"/>, or if <paramref name="value"/> consists exclusively of white-space characters.</returns>
  62. /// <param name="value">The string to test.</param>
  63. public static bool IsNullOrWhiteSpace(string value)
  64. {
  65. #if NET35
  66. if (value != null)
  67. {
  68. for (int index = 0; index < value.Length; ++index)
  69. {
  70. if (!char.IsWhiteSpace(value[index]))
  71. return false;
  72. }
  73. }
  74. return true;
  75. #else
  76. return string.IsNullOrWhiteSpace(value);
  77. #endif
  78. }
  79. #endregion // IsNullOrWhiteSpace
  80. #region UnitSizeToText
  81. /// <summary>Converts a number of type T to string, suffixed with a unit size.</summary>
  82. public static string UnitSizeToText<T>(T numberOfBytes)
  83. {
  84. string[] sizeFormats =
  85. {
  86. "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"
  87. };
  88. var i = 0;
  89. var bytes = Convert.ToDouble(numberOfBytes, CultureInfo.InvariantCulture);
  90. while (i < sizeFormats.Length && bytes > 1024)
  91. {
  92. i++;
  93. bytes /= 1024;
  94. }
  95. // Will return "512 B" instead of "512,00 B".
  96. return string.Format(CultureInfo.CurrentCulture, i == 0 ? "{0:0} {1}" : "{0:0.##} {1}", bytes, sizeFormats[i]);
  97. }
  98. /// <summary>Calculates a percentage value.</summary>
  99. /// <param name="currentValue"/>
  100. /// <param name="minimumValue"/>
  101. /// <param name="maximumValue"/>
  102. public static double PercentCalculate(double currentValue, double minimumValue, double maximumValue)
  103. {
  104. return (currentValue < 0 || maximumValue <= 0) ? 0 : currentValue * 100 / (maximumValue - minimumValue);
  105. }
  106. #endregion // UnitSizeToText
  107. }
  108. }