TypeExtension.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using SqlSugar;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Security.Cryptography;
  7. namespace WCS.Service.Extensions
  8. {
  9. public static class TypeExtension
  10. {
  11. /// <summary>
  12. /// 将字符串转换为short
  13. /// </summary>
  14. /// <param name="value">需要转换的字符串</param>
  15. /// <returns></returns>
  16. public static short ToShort(this string value)
  17. {
  18. return Convert.ToInt16(value);
  19. }
  20. /// <summary>
  21. /// 将int转换为short
  22. /// </summary>
  23. /// <param name="value">需要转换的字符串</param>
  24. /// <returns></returns>
  25. public static short ToShort(this int value)
  26. {
  27. return Convert.ToInt16(value);
  28. }
  29. /// <summary>
  30. /// 将decimal转换为short
  31. /// </summary>
  32. /// <param name="value">需要转换的字符串</param>
  33. /// <returns></returns>
  34. public static short ToShort(this decimal value)
  35. {
  36. return Convert.ToInt16(value);
  37. }
  38. /// <summary>
  39. /// 将字符串转换为int
  40. /// </summary>
  41. /// <param name="value">需要转换的字符串</param>
  42. /// <returns></returns>
  43. public static int ToInt(this string value)
  44. {
  45. return Convert.ToInt32(value);
  46. }
  47. /// <summary>
  48. /// 判断值为奇数/偶数
  49. /// </summary>
  50. /// <param name="value">需要判断的值</param>
  51. /// <returns> true:是奇数 false:是偶数</returns>
  52. public static bool OddNumberOrEven(this short value)
  53. {
  54. return value % 2 != 0;
  55. }
  56. /// <summary>
  57. /// 获取short类型Code,只限设备组
  58. /// </summary>
  59. /// <param name="value"></param>
  60. /// <returns></returns>
  61. public static short GetShortCode(this string value)
  62. {
  63. return value.Replace("G", "").ToShort();
  64. }
  65. /// <summary>
  66. /// 数据映射
  67. /// </summary>
  68. /// <typeparam name="D"></typeparam>
  69. /// <typeparam name="S"></typeparam>
  70. /// <param name="s"></param>
  71. /// <returns></returns>
  72. public static D Mapper<D, S>(S s)
  73. {
  74. D d = Activator.CreateInstance<D>();
  75. var sType = s.GetType();
  76. var dType = typeof(D);
  77. foreach (PropertyInfo sP in sType.GetProperties())
  78. {
  79. foreach (PropertyInfo dP in dType.GetProperties())
  80. {
  81. if (dP.Name == sP.Name)
  82. {
  83. dP.SetValue(d, sP.GetValue(s));
  84. break;
  85. }
  86. }
  87. }
  88. return d;
  89. }
  90. /// <summary>
  91. /// 获取字典
  92. /// </summary>
  93. /// <typeparam name="T1"></typeparam>
  94. /// <typeparam name="T2"></typeparam>
  95. /// <typeparam name="T3"></typeparam>
  96. /// <param name="t3"></param>
  97. /// <returns></returns>
  98. public static Dictionary<string, object> EntityClassToDictionary<T>(T t)
  99. {
  100. Type type = typeof(SugarColumn);
  101. Dictionary<string, object> d = new Dictionary<string, object>();
  102. var sType = t.GetType();
  103. foreach (PropertyInfo sP in sType.GetProperties())
  104. {
  105. if (sP.CustomAttributes.Any(v => v.AttributeType == type) && sP.Name != "VER" && sP.Name != "ID")
  106. {
  107. d.Add(sP.Name, sP.GetValue(t));
  108. }
  109. }
  110. return d;
  111. }
  112. /// <summary>
  113. /// 获取MD5字符串
  114. /// </summary>
  115. /// <param name="myString"></param>
  116. /// <returns></returns>
  117. public static string GetMD5(this string myString)
  118. {
  119. MD5 md5 = MD5.Create();
  120. byte[] fromData = System.Text.Encoding.Unicode.GetBytes(myString);
  121. byte[] targetData = md5.ComputeHash(fromData);
  122. string byte2String = null;
  123. for (int i = 0; i < targetData.Length; i++)
  124. {
  125. byte2String += targetData[i].ToString("x");
  126. }
  127. return byte2String;
  128. }
  129. }
  130. }