您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LuCoreUtilsExtensions.cs 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4. namespace Luticate2.Utils.Utils
  5. {
  6. public static class LuCoreUtilsExtensions
  7. {
  8. public static bool HasProperty(this Type type, string name)
  9. {
  10. return type.GetProperty(name) != null;
  11. }
  12. public static string ToSnakeCase(this string str)
  13. {
  14. if (str == null)
  15. {
  16. throw new ArgumentNullException(nameof(str));
  17. }
  18. return string.Concat(str.Select((x, i) => i > 0 && char.IsUpper(x) ? "_" + x.ToString() : x.ToString())).ToLower();
  19. }
  20. public static T ToOptEnum<T>(this string model)
  21. {
  22. return model == null ? default(T) : (T) Enum.Parse(typeof(T), model);
  23. }
  24. public static T ToEnum<T>(this string model)
  25. {
  26. return (T) Enum.Parse(typeof(T), model);
  27. }
  28. public static Guid? ToOptGuid(this string str)
  29. {
  30. return str == null ? (Guid?)null : new Guid(str);
  31. }
  32. public static Guid ToGuid(this string str)
  33. {
  34. return new Guid(str);
  35. }
  36. public static string ToDbo(this Guid model)
  37. {
  38. return model.ToString();
  39. }
  40. public static string ToDbo(this Guid? model)
  41. {
  42. return model?.ToDbo();
  43. }
  44. public static DateTime ToDbo(this DateTime model)
  45. {
  46. return model.ToLocalTime().ToUniversalTime();
  47. }
  48. public static DateTime? ToDbo(this DateTime? model)
  49. {
  50. return model?.ToDbo();
  51. }
  52. public static DateTime ToModel(this DateTime model)
  53. {
  54. return model.ToUniversalTime();
  55. }
  56. public static DateTime? ToModel(this DateTime? model)
  57. {
  58. return model?.ToModel();
  59. }
  60. }
  61. }