using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Globalization; using System.Linq; using System.Reflection; using System.Text.RegularExpressions; using System.Web; namespace Store.Framework.Extensions; public static class Extensions { public static long ParseToLong(this object obj) { try { string text = ParseToString(obj); if (string.IsNullOrEmpty(text)) { return 0L; } return long.Parse(text); } catch { return 0L; } } public static long ParseToLong(this string str, long defaultValue) { try { return long.Parse(str); } catch { return defaultValue; } } public static int ParseToInt(this object str) { try { return Convert.ToInt32(str); } catch { return 0; } } public static int DecimalPointParseToInt(this object str) { try { string text = ParseToString(str); int num = text.IndexOf('.'); if (num > 0) { text = text.Substring(0, num); } return Convert.ToInt32(text); } catch { return 0; } } public static int ParseToInt(this object str, int defaultValue) { if (str == null) { return defaultValue; } try { return Convert.ToInt32(str); } catch { return defaultValue; } } public static short ParseToShort(this object obj) { try { return short.Parse(obj.ToString()); } catch { return 0; } } public static short ParseToShort(this object str, short defaultValue) { try { return short.Parse(str.ToString()); } catch { return defaultValue; } } public static decimal ParseToDecimal(this object str, decimal defaultValue) { try { return decimal.Parse(str.ToString()); } catch { return defaultValue; } } public static decimal ParseToDecimal(this object str) { try { return decimal.Parse(str.ToString()); } catch { return 0m; } } public static bool ParseToBool(this object str) { try { return bool.Parse(str.ToString()); } catch { return false; } } public static bool ParseToBool(this object str, bool result) { try { return bool.Parse(str.ToString()); } catch { return result; } } public static float ParseToFloat(this object str) { try { return float.Parse(str.ToString()); } catch { return 0f; } } public static float ParseToFloat(this object str, float result) { try { return float.Parse(str.ToString()); } catch { return result; } } public static Guid ParseToGuid(this string str) { try { return new Guid(str); } catch { return Guid.Empty; } } public static DateTime ParseToDateTime(this string str) { try { if (string.IsNullOrWhiteSpace(str)) { return DateTime.MinValue; } if (str.Contains("-") || str.Contains("/")) { return DateTime.Parse(str); } return str.Length switch { 4 => DateTime.ParseExact(str, "yyyy", CultureInfo.CurrentCulture), 6 => DateTime.ParseExact(str, "yyyyMM", CultureInfo.CurrentCulture), 8 => DateTime.ParseExact(str, "yyyyMMdd", CultureInfo.CurrentCulture), 10 => DateTime.ParseExact(str, "yyyyMMddHH", CultureInfo.CurrentCulture), 12 => DateTime.ParseExact(str, "yyyyMMddHHmm", CultureInfo.CurrentCulture), 14 => DateTime.ParseExact(str, "yyyyMMddHHmmss", CultureInfo.CurrentCulture), _ => DateTime.ParseExact(str, "yyyyMMddHHmmss", CultureInfo.CurrentCulture), }; } catch { return DateTime.MinValue; } } public static DateTime ParseToDateTime(this string str, DateTime? defaultValue) { try { if (string.IsNullOrWhiteSpace(str)) { return defaultValue.GetValueOrDefault(); } if (str.Contains("-") || str.Contains("/")) { return DateTime.Parse(str); } return str.Length switch { 4 => DateTime.ParseExact(str, "yyyy", CultureInfo.CurrentCulture), 6 => DateTime.ParseExact(str, "yyyyMM", CultureInfo.CurrentCulture), 8 => DateTime.ParseExact(str, "yyyyMMdd", CultureInfo.CurrentCulture), 10 => DateTime.ParseExact(str, "yyyyMMddHH", CultureInfo.CurrentCulture), 12 => DateTime.ParseExact(str, "yyyyMMddHHmm", CultureInfo.CurrentCulture), 14 => DateTime.ParseExact(str, "yyyyMMddHHmmss", CultureInfo.CurrentCulture), _ => DateTime.ParseExact(str, "yyyyMMddHHmmss", CultureInfo.CurrentCulture), }; } catch { return defaultValue.GetValueOrDefault(); } } public static string ParseToString(this object obj) { try { if (obj == null) { return string.Empty; } return obj.ToString(); } catch { return string.Empty; } } public static string ParseToStrings(this object obj, string split = ",") { try { if (obj is IEnumerable values) { return string.Join(split, values); } return obj.ToString(); } catch { return string.Empty; } } public static double ParseToDouble(this object obj) { try { return double.Parse(obj.ToString()); } catch { return 0.0; } } public static double ParseToDouble(this object str, double defaultValue) { try { return double.Parse(str.ToString()); } catch { return defaultValue; } } public static T[] SplitToArray(this string value, string split = ",") { if (string.IsNullOrEmpty(value)) { return null; } return CastSuper(value.Split(new string[1] { split }, StringSplitOptions.RemoveEmptyEntries)).ToArray(); } public static T[] SplitToArray(this string value, string[] split) { if (string.IsNullOrEmpty(value)) { return null; } return CastSuper(value.Split(split, StringSplitOptions.RemoveEmptyEntries)).ToArray(); } public static List SplitToList(this string value, string split = ",") { if (string.IsNullOrEmpty(value)) { return new List(); } return CastSuper(value.Split(new string[1] { split }, StringSplitOptions.RemoveEmptyEntries)).ToList(); } public static List SplitToList(this string value, string[] split) { if (string.IsNullOrEmpty(value)) { return new List(); } return CastSuper(value.Split(split, StringSplitOptions.RemoveEmptyEntries)).ToList(); } public static IEnumerable CastSuper(this IEnumerable source) { foreach (object item in source) { yield return (TResult)Convert.ChangeType(item, typeof(TResult)); } } public static Dictionary EnumToDictionary(this Type enumType) { Dictionary dictionary = new Dictionary(); Type typeFromHandle = typeof(DescriptionAttribute); FieldInfo[] fields = enumType.GetFields(); int num = 0; string empty = string.Empty; FieldInfo[] array = fields; foreach (FieldInfo fieldInfo in array) { if (fieldInfo.FieldType.IsEnum) { num = (int)enumType.InvokeMember(fieldInfo.Name, BindingFlags.GetField, null, null, null); object[] customAttributes = fieldInfo.GetCustomAttributes(typeFromHandle, inherit: true); if (customAttributes.Length != 0) { DescriptionAttribute descriptionAttribute = (DescriptionAttribute)customAttributes[0]; empty = descriptionAttribute.Description; } else { empty = fieldInfo.Name; } dictionary.Add(num, empty); } } return dictionary.OrderBy((KeyValuePair p) => p.Key).ToDictionary((KeyValuePair p) => p.Key, (KeyValuePair p) => p.Value); } public static string GetDescription(this Enum enumType) { FieldInfo field = enumType.GetType().GetField(enumType.ToString()); if (field != null) { DescriptionAttribute[] array = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), inherit: false); if (array.Length != 0) { return array[0].Description; } } return enumType.ToString(); } public static string GetDescriptionByEnum(this object obj) { Enum enumType = Enum.Parse(typeof(T), ParseToString(obj)) as Enum; return GetDescription(enumType); } public static int GetEnumValueByDescription(this string description) { Dictionary source = EnumToDictionary(typeof(T)); return (from p in source where p.Value == description select p.Key).FirstOrDefault(); } public static int GetEnumValueByName(this string name) { return (int)Enum.Parse(typeof(T), name); } public static object GetEnumNameByValue(this int value) { return Enum.ToObject(typeof(T), value); } public static Exception GetOriginalException(this Exception ex) { if (ex.InnerException == null) { return ex; } return GetOriginalException(ex.InnerException); } public static bool IsNullOrEmpty(this object value) { if (value != null && !string.IsNullOrEmpty(ParseToString(value))) { return false; } return true; } public static bool IsNullOrZero(this object value) { if (value == null || string.IsNullOrEmpty(ParseToString(value)) || ParseToString(value).Trim() == "0") { return true; } return false; } public static bool IsNumericType(this object value) { if (value == null) { return false; } TypeCode typeCode = Type.GetTypeCode(value.GetType()); TypeCode typeCode2 = typeCode; if ((uint)(typeCode2 - 5) <= 10u) { return true; } return false; } public static bool IsStringType(this object value) { if (value == null) { return false; } TypeCode typeCode = Type.GetTypeCode(value.GetType()); TypeCode typeCode2 = typeCode; if (typeCode2 == TypeCode.String) { return true; } return false; } public static bool IsNumber(this string value) { return RegexHelper.IsNumber(value); } public static bool IsAlphabet(this string value) { return RegexHelper.IsAlphabet(value); } public static bool IsAlphabetOrNumber(this string value) { return RegexHelper.IsAlphabetOrNumber(value); } } public class RegexHelper { public static MatchCollection GetMatchCollection(string html, string regex) { if (string.IsNullOrEmpty(html)) { return null; } return Regex.Matches(html, regex, RegexOptions.IgnoreCase); } public static string GetMatch(string html, string regex, int index = 0) { if (string.IsNullOrEmpty(html)) { return string.Empty; } MatchCollection matchCollection = Regex.Matches(html, regex, RegexOptions.IgnoreCase); if (matchCollection.Count > 0) { return matchCollection[index].Groups[1].Value.ToString(); } return string.Empty; } public static string GetMatchWithSingleLine(string html, string regex) { if (string.IsNullOrEmpty(html)) { return string.Empty; } MatchCollection matchCollection = Regex.Matches(html, regex, RegexOptions.IgnoreCase | RegexOptions.Singleline); if (matchCollection.Count > 0) { return matchCollection[0].Groups[1].ToString(); } return string.Empty; } public static bool IsNumber(string str) { Regex regex = new Regex("^[0-9\\.]+$"); return regex.IsMatch(str); } public static bool IsAlphabet(string str) { Regex regex = new Regex("^[A-Za-z]+$"); return regex.IsMatch(str); } public static bool IsAlphabetOrNumber(string str) { Regex regex = new Regex("^[A-Za-z0-9]+$"); return regex.IsMatch(str); } }