添加项目文件。
This commit is contained in:
@ -0,0 +1,88 @@
|
||||
using System;
|
||||
|
||||
namespace QYZH.InteractiveMagazine.Common.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 日期时间扩展方法
|
||||
/// </summary>
|
||||
public static class DateTimeExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 转换为Unix时间戳(秒)
|
||||
/// </summary>
|
||||
/// <param name="dateTime">日期时间</param>
|
||||
/// <returns>Unix时间戳</returns>
|
||||
public static long ToTimestamp(this DateTime dateTime)
|
||||
{
|
||||
return new DateTimeOffset(dateTime.ToUniversalTime()).ToUnixTimeSeconds();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从Unix时间戳(秒)转换为DateTime
|
||||
/// </summary>
|
||||
/// <param name="timestamp">Unix时间戳</param>
|
||||
/// <returns>日期时间</returns>
|
||||
public static DateTime FromTimestamp(long timestamp)
|
||||
{
|
||||
return DateTimeOffset.FromUnixTimeSeconds(timestamp).LocalDateTime;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为指定格式的日期时间字符串
|
||||
/// </summary>
|
||||
/// <param name="dateTime">日期时间</param>
|
||||
/// <param name="format">格式字符串,默认为"yyyy-MM-dd HH:mm:ss"</param>
|
||||
/// <returns>格式化的日期时间字符串</returns>
|
||||
public static string ToDateTimeString(this DateTime dateTime, string format = "yyyy-MM-dd HH:mm:ss")
|
||||
{
|
||||
return dateTime.ToString(format);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从生日计算年龄
|
||||
/// </summary>
|
||||
/// <param name="birthday">生日日期</param>
|
||||
/// <returns>年龄</returns>
|
||||
public static int GetAge(this DateTime birthday)
|
||||
{
|
||||
int age = DateTime.Now.Year - birthday.Year;
|
||||
|
||||
if (DateTime.Now.DayOfYear < birthday.DayOfYear)
|
||||
{
|
||||
age--;
|
||||
}
|
||||
|
||||
return age;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断是否是今天
|
||||
/// </summary>
|
||||
/// <param name="dateTime">日期时间</param>
|
||||
/// <returns>是否是今天</returns>
|
||||
public static bool IsToday(this DateTime dateTime)
|
||||
{
|
||||
return dateTime.Date == DateTime.Today;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当天开始时间(00:00:00)
|
||||
/// </summary>
|
||||
/// <param name="dateTime">日期时间</param>
|
||||
/// <returns>当天开始时间</returns>
|
||||
public static DateTime ToStartOfDay(this DateTime dateTime)
|
||||
{
|
||||
return dateTime.Date;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当天结束时间(23:59:59.999)
|
||||
/// </summary>
|
||||
/// <param name="dateTime">日期时间</param>
|
||||
/// <returns>当天结束时间</returns>
|
||||
public static DateTime ToEndOfDay(this DateTime dateTime)
|
||||
{
|
||||
return dateTime.Date.AddDays(1).AddTicks(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
79
QYZH.InteractiveMagazine.Common/Extensions/EnumExtension.cs
Normal file
79
QYZH.InteractiveMagazine.Common/Extensions/EnumExtension.cs
Normal file
@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
|
||||
namespace QYZH.InteractiveMagazine.Common.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 枚举扩展方法
|
||||
/// </summary>
|
||||
public static class EnumExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取枚举的DescriptionAttribute描述
|
||||
/// </summary>
|
||||
/// <typeparam name="T">枚举类型</typeparam>
|
||||
/// <param name="enumValue">枚举值</param>
|
||||
/// <returns>描述文本,如果没有DescriptionAttribute则返回枚举名称</returns>
|
||||
public static string GetDescription<T>(this T enumValue) where T : Enum
|
||||
{
|
||||
System.Reflection.FieldInfo? field = enumValue.GetType().GetField(enumValue.ToString());
|
||||
if (field == null)
|
||||
{
|
||||
return enumValue.ToString();
|
||||
}
|
||||
|
||||
var attribute = (DescriptionAttribute?)Attribute.GetCustomAttribute(
|
||||
field, typeof(DescriptionAttribute));
|
||||
|
||||
return attribute?.Description ?? enumValue.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取枚举名称
|
||||
/// </summary>
|
||||
/// <typeparam name="T">枚举类型</typeparam>
|
||||
/// <param name="enumValue">枚举值</param>
|
||||
/// <returns>枚举名称</returns>
|
||||
public static string GetName<T>(this T enumValue) where T : Enum
|
||||
{
|
||||
return enumValue.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有枚举名称列表
|
||||
/// </summary>
|
||||
/// <typeparam name="T">枚举类型</typeparam>
|
||||
/// <returns>枚举名称列表</returns>
|
||||
public static List<string> GetNames<T>() where T : Enum
|
||||
{
|
||||
return Enum.GetNames(typeof(T)).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有枚举描述列表
|
||||
/// </summary>
|
||||
/// <typeparam name="T">枚举类型</typeparam>
|
||||
/// <returns>枚举描述列表</returns>
|
||||
public static List<string> GetDescriptions<T>() where T : Enum
|
||||
{
|
||||
return Enum.GetValues(typeof(T))
|
||||
.Cast<T>()
|
||||
.Select(e => e.GetDescription())
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将枚举转换为字典(名称,值)
|
||||
/// </summary>
|
||||
/// <typeparam name="T">枚举类型</typeparam>
|
||||
/// <returns>枚举字典</returns>
|
||||
public static Dictionary<string, int> ToDictionary<T>() where T : Enum
|
||||
{
|
||||
return Enum.GetValues(typeof(T))
|
||||
.Cast<T>()
|
||||
.ToDictionary(e => e.GetDescription(), e => Convert.ToInt32(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,94 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace QYZH.InteractiveMagazine.Common.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 对象扩展方法
|
||||
/// </summary>
|
||||
public static class ObjectExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 将源对象的属性值拷贝到目标对象
|
||||
/// </summary>
|
||||
/// <typeparam name="T">目标对象类型</typeparam>
|
||||
/// <param name="source">源对象</param>
|
||||
/// <param name="target">目标对象</param>
|
||||
/// <returns>目标对象</returns>
|
||||
public static T CopyTo<T>(this object source, T target)
|
||||
{
|
||||
if (source == null || target == null)
|
||||
{
|
||||
return target;
|
||||
}
|
||||
|
||||
Type sourceType = source.GetType();
|
||||
Type targetType = target.GetType();
|
||||
|
||||
PropertyInfo[] sourceProperties = sourceType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
||||
|
||||
foreach (PropertyInfo sourceProp in sourceProperties)
|
||||
{
|
||||
if (!sourceProp.CanRead)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
PropertyInfo? targetProp = targetType.GetProperty(sourceProp.Name);
|
||||
|
||||
if (targetProp != null && targetProp.CanWrite &&
|
||||
targetProp.PropertyType == sourceProp.PropertyType)
|
||||
{
|
||||
object? value = sourceProp.GetValue(source);
|
||||
targetProp.SetValue(target, value);
|
||||
}
|
||||
}
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将对象转换为JSON字符串
|
||||
/// </summary>
|
||||
/// <param name="obj">对象</param>
|
||||
/// <param name="formatting">格式化选项</param>
|
||||
/// <returns>JSON字符串</returns>
|
||||
public static string ToJson(this object obj, Formatting formatting = Formatting.None)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
return JsonConvert.SerializeObject(obj, formatting);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将对象转换为字典
|
||||
/// </summary>
|
||||
/// <param name="obj">对象</param>
|
||||
/// <returns>字典</returns>
|
||||
public static Dictionary<string, object?> ToDictionary(this object obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
return new Dictionary<string, object?>();
|
||||
}
|
||||
|
||||
var dictionary = new Dictionary<string, object?>();
|
||||
PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
||||
|
||||
foreach (PropertyInfo property in properties)
|
||||
{
|
||||
if (property.CanRead)
|
||||
{
|
||||
dictionary[property.Name] = property.GetValue(obj);
|
||||
}
|
||||
}
|
||||
|
||||
return dictionary;
|
||||
}
|
||||
}
|
||||
}
|
||||
150
QYZH.InteractiveMagazine.Common/Extensions/StringExtension.cs
Normal file
150
QYZH.InteractiveMagazine.Common/Extensions/StringExtension.cs
Normal file
@ -0,0 +1,150 @@
|
||||
using System;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace QYZH.InteractiveMagazine.Common.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 字符串扩展方法
|
||||
/// </summary>
|
||||
public static class StringExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// MD5加密
|
||||
/// </summary>
|
||||
/// <param name="input">原始字符串</param>
|
||||
/// <returns>MD5哈希值(32位小写)</returns>
|
||||
public static string ToMd5(this string input)
|
||||
{
|
||||
if (string.IsNullOrEmpty(input))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
using (var md5 = MD5.Create())
|
||||
{
|
||||
byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
|
||||
var sb = new StringBuilder();
|
||||
foreach (byte b in bytes)
|
||||
{
|
||||
sb.Append(b.ToString("x2"));
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SHA256加密
|
||||
/// </summary>
|
||||
/// <param name="input">原始字符串</param>
|
||||
/// <returns>SHA256哈希值(64位小写)</returns>
|
||||
public static string ToSha256(this string input)
|
||||
{
|
||||
if (string.IsNullOrEmpty(input))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
using (var sha256 = SHA256.Create())
|
||||
{
|
||||
byte[] bytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(input));
|
||||
var sb = new StringBuilder();
|
||||
foreach (byte b in bytes)
|
||||
{
|
||||
sb.Append(b.ToString("x2"));
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base64编码
|
||||
/// </summary>
|
||||
/// <param name="input">原始字符串</param>
|
||||
/// <returns>Base64编码后的字符串</returns>
|
||||
public static string ToBase64(this string input)
|
||||
{
|
||||
if (string.IsNullOrEmpty(input))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
byte[] bytes = Encoding.UTF8.GetBytes(input);
|
||||
return Convert.ToBase64String(bytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base64解码
|
||||
/// </summary>
|
||||
/// <param name="input">Base64编码的字符串</param>
|
||||
/// <returns>解码后的原始字符串</returns>
|
||||
public static string FromBase64(this string input)
|
||||
{
|
||||
if (string.IsNullOrEmpty(input))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
byte[] bytes = Convert.FromBase64String(input);
|
||||
return Encoding.UTF8.GetString(bytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断字符串是否为空或空白
|
||||
/// </summary>
|
||||
/// <param name="input">待检查的字符串</param>
|
||||
/// <returns>是否为空或空白</returns>
|
||||
public static bool IsNullOrWhiteSpace(this string input)
|
||||
{
|
||||
return string.IsNullOrWhiteSpace(input);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 手机号脱敏
|
||||
/// </summary>
|
||||
/// <param name="phone">手机号</param>
|
||||
/// <returns>脱敏后的手机号(中间4位用*替换)</returns>
|
||||
public static string MaskPhone(this string phone)
|
||||
{
|
||||
if (string.IsNullOrEmpty(phone) || phone.Length < 7)
|
||||
{
|
||||
return phone;
|
||||
}
|
||||
|
||||
return phone.Substring(0, 3) + "****" + phone.Substring(phone.Length - 4);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 身份证脱敏
|
||||
/// </summary>
|
||||
/// <param name="idCard">身份证号</param>
|
||||
/// <returns>脱敏后的身份证号(保留前3位和后4位)</returns>
|
||||
public static string MaskIdCard(this string idCard)
|
||||
{
|
||||
if (string.IsNullOrEmpty(idCard) || idCard.Length < 7)
|
||||
{
|
||||
return idCard;
|
||||
}
|
||||
|
||||
int length = idCard.Length;
|
||||
return idCard.Substring(0, 3) + new string('*', length - 7) + idCard.Substring(length - 4);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 正则匹配
|
||||
/// </summary>
|
||||
/// <param name="input">待匹配的字符串</param>
|
||||
/// <param name="pattern">正则表达式</param>
|
||||
/// <returns>是否匹配</returns>
|
||||
public static bool IsMatchRegex(this string input, string pattern)
|
||||
{
|
||||
if (string.IsNullOrEmpty(input) || string.IsNullOrEmpty(pattern))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return Regex.IsMatch(input, pattern);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user