using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyCode.Project.Infrastructure.Common;
using MyCode.Project.Infrastructure.Enumeration;
namespace MyCode.Project.Infrastructure.Extensions
{
///
/// 公共扩展
///
public static partial class Extensions
{
#region IsEmpty(Guid是否为空)
///
/// Guid 是否为空
///
/// 值
///
public static bool IsEmpty(this Guid value)
{
if (value == Guid.Empty)
{
return true;
}
return false;
}
#endregion
#region IsEmpty(Guid? 是否为空)
///
/// Guid? 是否为空
///
/// 值
///
public static bool IsEmpty(this Guid? value)
{
if (value == null)
{
return true;
}
return IsEmpty(value.Value);
}
#endregion
#region IsEmpty(判断 数组 是否为空)
///
/// 判断 数组 是否为空
///
/// 数据
///
public static bool IsEmpty(this Array array)
{
return array == null || array.Length == 0;
}
#endregion
#region IsEmpty(判断 可变字符串 是否为空)
///
/// 判断 可变字符串 是否为空
///
/// 数据
///
public static bool IsEmpty(this StringBuilder sb)
{
return sb == null || sb.Length == 0 || sb.ToString().IsEmpty();
}
#endregion
#region IsEmpty(判断 泛型集合 是否为空)
///
/// 判断 泛型集合 是否为空
///
/// 泛型对象
/// 数据
///
public static bool IsEmpty(this ICollection list)
{
return null == list || list.Count == 0;
}
#endregion
#region IsEmpty(判断 迭代集合 是否为空)
///
/// 判断 迭代集合 是否为空
///
/// 泛型对象
/// 数据
///
public static bool IsEmpty(this IEnumerable list)
{
return null == list || !list.Any();
}
#endregion
#region IsEmpty(判断字典是否为空)
///
/// 判断 字典 是否为空
///
/// 键类型
/// 值类型
/// 数据
///
public static bool IsEmpty(this IDictionary dictionary)
{
return null == dictionary || dictionary.Count == 0;
}
#endregion
#region GetFirstDayOfMonth(获取指定日期的月份第一天)
///
/// 获取指定日期的月份第一天
///
/// 日期
/// 月份第一天
public static DateTime GetFirstDayOfMonth(this DateTime date)
{
return new DateTime(date.Year, date.Month, 1);
}
///
/// 获取指定日期的月份第一天,指定星期几
///
/// 日期
/// 星期几
/// 月份第一天
public static DateTime GetFirstDayOfMonth(this DateTime date, DayOfWeek dayOfWeek)
{
var dt = date.GetFirstDayOfMonth();
while (dt.DayOfWeek != dayOfWeek)
dt = dt.AddDays(1);
return dt;
}
#endregion
#region GetLastDayOfMonth(获取指定日期的月份最后一天)
///
/// 获取指定日期的月份最后一天
///
/// 日期
/// 最后一天
public static DateTime GetLastDayOfMonth(this DateTime date)
{
return new DateTime(date.Year, date.Month, GetCountDaysOfMonth(date));
}
///
/// 获取指定日期的月份最后一天,指定星期几
///
/// 日期
/// 星期几
/// 最后一天
public static DateTime GetLastDayOfMonth(this DateTime date, DayOfWeek dayOfWeek)
{
var dt = date.GetLastDayOfMonth();
while (dt.DayOfWeek != dayOfWeek)
dt = dt.AddDays(-1);
return dt;
}
#endregion
#region GetCountDaysOfMonth(获取月总天数)
///
/// 获取月总天数
///
/// 日期
/// 月总天数
public static int GetCountDaysOfMonth(this DateTime date)
{
var nextMonth = date.AddMonths(1);
return new DateTime(nextMonth.Year, nextMonth.Month, 1).AddDays(-1).Day;
}
#endregion
#region ToDataTable(将List转换成数据表)
///
/// 将List转换成数据表
///
/// 实体类型
/// List集合
///
public static DataTable ToDataTable(this List entities) where T : class
{
DataTable dt = new DataTable();
var properties = typeof(T).GetProperties().ToList();
properties.ForEach(item =>
{
Type colType = item.PropertyType;
if ((colType.IsGenericType) && colType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
colType = colType.GetGenericArguments()[0];
}
dt.Columns.Add(new DataColumn(item.Name) {DataType = colType});
});
entities.ToList().ForEach(item =>
{
var dr = dt.NewRow();
properties.ForEach(property =>
{
var value = property.GetValue(item, null);
dr[property.Name] = value ?? DBNull.Value;
});
dt.Rows.Add(dr);
});
return dt;
}
#endregion
#region SafeValue(安全返回值)
///
/// 安全返回值
///
/// 值类型
/// 可空值
///
public static T SafeValue(this T? value) where T : struct
{
return value ?? default(T);
}
#endregion
#region SafeString(安全转换为字符串)
///
/// 安全转换为字符串,去除两端空格,当值为null时返回""
///
/// 输入值
///
public static string SafeString(this object input)
{
return input == null ? string.Empty : input.ToString().Trim();
}
#endregion
#region Value(获取成员值)
///
/// 获取成员值
///
/// 枚举实例
///
public static int Value(this Enum instance)
{
return EnumHelper.GetValue(instance.GetType(), instance);
}
#endregion
}
}