using System; using System.Linq; using System.Reflection; namespace Luticate2.Utils.Utils { public static class LuCoreUtilsExtensions { public static bool HasProperty(this Type type, string name) { return type.GetProperty(name) != null; } public static string ToSnakeCase(this string str) { if (str == null) { throw new ArgumentNullException(nameof(str)); } return string.Concat(str.Select((x, i) => i > 0 && char.IsUpper(x) ? "_" + x.ToString() : x.ToString())).ToLower(); } public static T ToOptEnum(this string model) { return model == null ? default(T) : (T) Enum.Parse(typeof(T), model); } public static T ToEnum(this string model) { return (T) Enum.Parse(typeof(T), model); } public static Guid? ToOptGuid(this string str) { return str == null ? (Guid?)null : new Guid(str); } public static Guid ToGuid(this string str) { return new Guid(str); } public static string ToDbo(this Guid model) { return model.ToString(); } public static string ToDbo(this Guid? model) { return model?.ToDbo(); } public static DateTime ToDbo(this DateTime model) { return model.ToLocalTime().ToUniversalTime(); } public static DateTime? ToDbo(this DateTime? model) { return model?.ToDbo(); } public static DateTime ToModel(this DateTime model) { return model.ToUniversalTime(); } public static DateTime? ToModel(this DateTime? model) { return model?.ToModel(); } } }