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

LuCoreUtilsExtensions.cs 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 ToEnum<T>(this string model)
  21. {
  22. return (T) Enum.Parse(typeof(T), model);
  23. }
  24. public static Guid? ToGuid(this string str)
  25. {
  26. return str == null ? (Guid?)null : new Guid(str);
  27. }
  28. public static string ToDbo(this Guid model)
  29. {
  30. return model.ToString();
  31. }
  32. public static string ToDbo(this Guid? model)
  33. {
  34. return model?.ToDbo();
  35. }
  36. public static DateTime ToDbo(this DateTime model)
  37. {
  38. return model.ToLocalTime().ToUniversalTime();
  39. }
  40. public static DateTime? ToDbo(this DateTime? model)
  41. {
  42. return model?.ToDbo();
  43. }
  44. public static DateTime ToModel(this DateTime model)
  45. {
  46. return model.ToUniversalTime();
  47. }
  48. public static DateTime? ToModel(this DateTime? model)
  49. {
  50. return model?.ToModel();
  51. }
  52. }
  53. }