Helper.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Media.Imaging;
  8. namespace NEIntelligentControl2.Models
  9. {
  10. public static class Helper
  11. {
  12. /// <summary>
  13. /// 获取字符串的配置值
  14. /// </summary>
  15. /// <param name="cfgname"></param>
  16. /// <returns></returns>
  17. public static string GetConfiguration(this string cfgname)
  18. {
  19. try
  20. {
  21. return System.Configuration.ConfigurationManager.AppSettings[cfgname];
  22. }
  23. catch { }
  24. return "";
  25. }
  26. /// <summary>
  27. /// 从时间戳获取时间字符串
  28. /// </summary>
  29. /// <param name="l">时间戳</param>
  30. /// <returns>时间字符串</returns>
  31. public static string GetTimeString(this long l)
  32. {
  33. if (l <= 10000) return "";
  34. DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
  35. return startTime.AddMilliseconds(l).ToString("yyyy-MM-dd hh:mm:ss");
  36. }
  37. /// <summary>
  38. /// 从时间戳获取时间字符串
  39. /// </summary>
  40. public static string GetTimeSpanString(this long l)
  41. {
  42. if (l < 10000) return "";
  43. DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
  44. startTime = startTime.AddMilliseconds(l);
  45. TimeSpan tts = DateTime.Now - startTime;
  46. var ll = (long)tts.TotalMinutes;
  47. ll = ll < 0 ? 0 : ll;
  48. return ll.ToString();
  49. }
  50. /// <summary>
  51. /// 获取实例中的值
  52. /// </summary>
  53. public static object GetValue(this object op, string name)
  54. {
  55. string[] names = name.Split('.');
  56. object obj = null;
  57. if (names.Length == 1)
  58. {
  59. obj = op.GetType().GetProperty(name)?.GetValue(op);
  60. }
  61. else
  62. {
  63. var v1 = op.GetType().GetProperty(names[0])?.GetValue(op);
  64. if (v1 != null)
  65. obj = v1.GetType().GetProperty(names[1])?.GetValue(v1);
  66. }
  67. return obj;
  68. }
  69. /// <summary>
  70. /// 获取枚举的字符
  71. /// </summary>
  72. public static string GetStringValue(this System.Enum value)
  73. {
  74. string str = "";
  75. var type = value.GetType();
  76. var fi = type.GetField(value.ToString());
  77. var attrs = fi.GetCustomAttributes(typeof(Attributes.StringValue), false) as Attributes.StringValue[];
  78. if(attrs.Length > 0)
  79. {
  80. str = attrs[0].Value;
  81. }
  82. return str;
  83. }
  84. /// <summary>
  85. /// 从时间获取时间戳值
  86. /// </summary>
  87. /// <param name="dateTime"></param>
  88. /// <returns></returns>
  89. public static long GetTimeSpan(this DateTime dateTime)
  90. {
  91. DateTime dt = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
  92. TimeSpan time = dateTime.Subtract(dt);
  93. return long.Parse(time.Ticks.ToString().Substring(0, time.Ticks.ToString().Length - 4));
  94. }
  95. /// <summary>
  96. /// 从时间戳获取时间
  97. /// </summary>
  98. public static DateTime GetLongDateTime(this long utc)
  99. {
  100. DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
  101. startTime = startTime.AddMilliseconds(utc);
  102. return startTime;
  103. }
  104. public static BitmapImage GetBitmapImage(this Bitmap obj)
  105. {
  106. using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
  107. {
  108. obj.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
  109. var bs = ms.GetBuffer();
  110. BitmapImage bi = new BitmapImage();
  111. bi.BeginInit();
  112. bi.StreamSource = new System.IO.MemoryStream(bs);
  113. bi.EndInit();
  114. return bi;
  115. }
  116. }
  117. }
  118. }