MyExtensions.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using Abp.Extensions;
  2. using Newtonsoft.Json;
  3. using System.ComponentModel.DataAnnotations.Schema;
  4. using System.Data;
  5. using System.Reflection;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. namespace SnapshotWinFormsApp.Tools;
  9. public static class MyExtensions
  10. {
  11. public static bool IsNullOrEmpty(this string? str)
  12. {
  13. return string.IsNullOrEmpty(str);
  14. }
  15. public static bool NotNullOrEmpty(this string? str)
  16. {
  17. return !string.IsNullOrEmpty(str);
  18. }
  19. /// <summary>
  20. /// 获取字符串的 md5
  21. /// </summary>
  22. /// <param name="value"> 字符串 </param>
  23. /// <returns> MD5 后的字符串<see cref="string"/>. </returns>
  24. public static string GetMD5(this string value)
  25. {
  26. using (var md5 = MD5.Create())
  27. {
  28. return BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(value))).Replace("-", string.Empty);
  29. }
  30. }
  31. public static T FromJson<T>(this string json)
  32. {
  33. return json.IsNullOrEmpty() ? default(T) : JsonConvert.DeserializeObject<T>(json);
  34. }
  35. public static string GetTableName<T>(this T value) where T : class
  36. {
  37. var tableAttribute = typeof(T).GetCustomAttribute<TableAttribute>();
  38. return tableAttribute?.Name ?? string.Empty;
  39. }
  40. /// <summary>
  41. /// 将DataTable转换为指定类型的实体集合
  42. /// </summary>
  43. /// <typeparam name="T">实体类型</typeparam>
  44. /// <param name="dt">DataTable</param>
  45. /// <returns>实体集合</returns>
  46. public static List<T> ToList<T>(this DataTable dt) where T : new()
  47. {
  48. List<T> list = new List<T>();
  49. if (dt == null || dt.Rows.Count == 0)
  50. return list;
  51. PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty);
  52. foreach (DataRow row in dt.Rows)
  53. {
  54. T item = new T();
  55. foreach (PropertyInfo property in properties)
  56. {
  57. if (dt.Columns.Contains(property.Name) && !row.IsNull(property.Name))
  58. {
  59. object value = Convert.ChangeType(row[property.Name], property.PropertyType);
  60. property.SetValue(item, value, null);
  61. }
  62. }
  63. list.Add(item);
  64. }
  65. return list;
  66. }
  67. public static string ToSqlInsert<T>(this T entity) where T : class
  68. {
  69. if (entity == null)
  70. throw new ArgumentNullException(nameof(entity));
  71. var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
  72. var tableName = entity.GetTableName();
  73. var columns = new StringBuilder();
  74. var values = new StringBuilder();
  75. foreach (var property in properties)
  76. {
  77. if (!property.CanWrite)
  78. continue;
  79. var columnName = property.Name;
  80. var value = property.GetValue(entity);
  81. if (value == null)
  82. {
  83. //values.Append("NULL, ");
  84. continue;
  85. }
  86. if (property.PropertyType == typeof(string) || property.PropertyType == typeof(double))
  87. {
  88. value = $"'{value.ToString().Replace("'", "''")}'";
  89. }
  90. else if (property.PropertyType == typeof(DateTime) || property.PropertyType == typeof(DateTime?))
  91. {
  92. value = $"'{(DateTime)value:yyyy-MM-dd hh:mm:ss.ffffff}'";
  93. }
  94. else if (property.PropertyType.BaseType == typeof(Enum))
  95. {
  96. value = $"{(int)value}";
  97. }
  98. else
  99. {
  100. value = value.ToString();
  101. }
  102. columns.Append($"\"{columnName}\", ");
  103. values.Append($"{value}, ");
  104. }
  105. if (columns.Length == 0)
  106. throw new InvalidOperationException("没有可插入的属性。");
  107. // 移除最后的逗号和空格
  108. columns.Length -= 2;
  109. values.Length -= 2;
  110. var sql = $"INSERT INTO {tableName} ({columns}) VALUES ({values});";
  111. return sql;
  112. }
  113. }