添加项目文件。
This commit is contained in:
788
WaterCloud.Code/Extend/Ext.Convert.cs
Normal file
788
WaterCloud.Code/Extend/Ext.Convert.cs
Normal file
@@ -0,0 +1,788 @@
|
||||
/*******************************************************************************
|
||||
* Copyright © 2016 WaterCloud.Framework 版权所有
|
||||
* Author: WaterCloud
|
||||
* Description: WaterCloud快速开发平台
|
||||
* Website:
|
||||
*********************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace WaterCloud.Code
|
||||
{
|
||||
public static partial class Extensions
|
||||
{
|
||||
#region 数值转换
|
||||
|
||||
/// <summary>
|
||||
/// 转换为整型
|
||||
/// </summary>
|
||||
/// <param name="data">数据</param>
|
||||
public static int ToInt(this object data)
|
||||
{
|
||||
if (data == null)
|
||||
return 0;
|
||||
int result;
|
||||
var success = int.TryParse(data.ToString(), out result);
|
||||
if (success)
|
||||
return result;
|
||||
try
|
||||
{
|
||||
return Convert.ToInt32(ToDouble(data, 0));
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为long,若转换失败,则返回0。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static long ToLong(this object obj)
|
||||
{
|
||||
try
|
||||
{
|
||||
return long.Parse(obj.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为可空整型
|
||||
/// </summary>
|
||||
/// <param name="data">数据</param>
|
||||
public static int? ToIntOrNull(this object data)
|
||||
{
|
||||
if (data == null)
|
||||
return null;
|
||||
int result;
|
||||
bool isValid = int.TryParse(data.ToString(), out result);
|
||||
if (isValid)
|
||||
return result;
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为双精度浮点数
|
||||
/// </summary>
|
||||
/// <param name="data">数据</param>
|
||||
public static double ToDouble(this object data)
|
||||
{
|
||||
if (data == null)
|
||||
return 0;
|
||||
double result;
|
||||
return double.TryParse(data.ToString(), out result) ? result : 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为双精度浮点数,并按指定的小数位4舍5入
|
||||
/// </summary>
|
||||
/// <param name="data">数据</param>
|
||||
/// <param name="digits">小数位数</param>
|
||||
public static double ToDouble(this object data, int digits)
|
||||
{
|
||||
return Math.Round(ToDouble(data), digits);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为可空双精度浮点数
|
||||
/// </summary>
|
||||
/// <param name="data">数据</param>
|
||||
public static double? ToDoubleOrNull(this object data)
|
||||
{
|
||||
if (data == null)
|
||||
return null;
|
||||
double result;
|
||||
bool isValid = double.TryParse(data.ToString(), out result);
|
||||
if (isValid)
|
||||
return result;
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为高精度浮点数
|
||||
/// </summary>
|
||||
/// <param name="data">数据</param>
|
||||
public static decimal ToDecimal(this object data)
|
||||
{
|
||||
if (data == null)
|
||||
return 0;
|
||||
decimal result;
|
||||
return decimal.TryParse(data.ToString(), out result) ? result : 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为高精度浮点数,并按指定的小数位4舍5入
|
||||
/// </summary>
|
||||
/// <param name="data">数据</param>
|
||||
/// <param name="digits">小数位数</param>
|
||||
public static decimal ToDecimal(this object data, int digits)
|
||||
{
|
||||
return Math.Round(ToDecimal(data), digits);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为可空高精度浮点数
|
||||
/// </summary>
|
||||
/// <param name="data">数据</param>
|
||||
public static decimal? ToDecimalOrNull(this object data)
|
||||
{
|
||||
if (data == null)
|
||||
return null;
|
||||
decimal result;
|
||||
bool isValid = decimal.TryParse(data.ToString(), out result);
|
||||
if (isValid)
|
||||
return result;
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为可空高精度浮点数,并按指定的小数位4舍5入
|
||||
/// </summary>
|
||||
/// <param name="data">数据</param>
|
||||
/// <param name="digits">小数位数</param>
|
||||
public static decimal? ToDecimalOrNull(this object data, int digits)
|
||||
{
|
||||
var result = ToDecimalOrNull(data);
|
||||
if (result == null)
|
||||
return null;
|
||||
return Math.Round(result.Value, digits);
|
||||
}
|
||||
|
||||
#endregion 数值转换
|
||||
|
||||
#region 日期转换
|
||||
|
||||
/// <summary>
|
||||
/// 转换为日期
|
||||
/// </summary>
|
||||
/// <param name="data">数据</param>
|
||||
public static DateTime ToDate(this object data)
|
||||
{
|
||||
if (data == null)
|
||||
return DateTime.MinValue;
|
||||
DateTime result;
|
||||
return DateTime.TryParse(data.ToString(), out result) ? result : DateTime.MinValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为可空日期
|
||||
/// </summary>
|
||||
/// <param name="data">数据</param>
|
||||
public static DateTime? ToDateOrNull(this object data)
|
||||
{
|
||||
if (data == null)
|
||||
return null;
|
||||
DateTime result;
|
||||
bool isValid = DateTime.TryParse(data.ToString(), out result);
|
||||
if (isValid)
|
||||
return result;
|
||||
return null;
|
||||
}
|
||||
|
||||
#endregion 日期转换
|
||||
|
||||
#region 布尔转换
|
||||
|
||||
/// <summary>
|
||||
/// 转换为布尔值
|
||||
/// </summary>
|
||||
/// <param name="data">数据</param>
|
||||
public static bool ToBool(this object data)
|
||||
{
|
||||
if (data == null)
|
||||
return false;
|
||||
bool? value = GetBool(data);
|
||||
if (value != null)
|
||||
return value.Value;
|
||||
bool result;
|
||||
return bool.TryParse(data.ToString(), out result) && result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取布尔值
|
||||
/// </summary>
|
||||
private static bool? GetBool(this object data)
|
||||
{
|
||||
switch (data.ToString().Trim().ToLower())
|
||||
{
|
||||
case "0":
|
||||
return false;
|
||||
|
||||
case "1":
|
||||
return true;
|
||||
|
||||
case "是":
|
||||
return true;
|
||||
|
||||
case "否":
|
||||
return false;
|
||||
|
||||
case "yes":
|
||||
return true;
|
||||
|
||||
case "no":
|
||||
return false;
|
||||
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为可空布尔值
|
||||
/// </summary>
|
||||
/// <param name="data">数据</param>
|
||||
public static bool? ToBoolOrNull(this object data)
|
||||
{
|
||||
if (data == null)
|
||||
return null;
|
||||
bool? value = GetBool(data);
|
||||
if (value != null)
|
||||
return value.Value;
|
||||
bool result;
|
||||
bool isValid = bool.TryParse(data.ToString(), out result);
|
||||
if (isValid)
|
||||
return result;
|
||||
return null;
|
||||
}
|
||||
|
||||
#endregion 布尔转换
|
||||
|
||||
#region 是否为空
|
||||
|
||||
/// <summary>
|
||||
/// 是否为空
|
||||
/// </summary>
|
||||
/// <param name="value">值</param>
|
||||
public static bool IsEmpty(this string value)
|
||||
{
|
||||
return string.IsNullOrWhiteSpace(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否为空
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsEmpty(this object value)
|
||||
{
|
||||
if (value != null && !string.IsNullOrEmpty(value.ToString()))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否为空
|
||||
/// </summary>
|
||||
/// <param name="value">值</param>
|
||||
public static bool IsEmpty(this Guid? value)
|
||||
{
|
||||
if (value == null)
|
||||
return true;
|
||||
return IsEmpty(value.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否为空
|
||||
/// </summary>
|
||||
/// <param name="value">值</param>
|
||||
public static bool IsEmpty(this Guid value)
|
||||
{
|
||||
if (value == Guid.Empty)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
#endregion 是否为空
|
||||
|
||||
#region 强制转换类型
|
||||
|
||||
/// <summary>
|
||||
/// 强制转换类型
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult"></typeparam>
|
||||
/// <param name="source"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<TResult> CastSuper<TResult>(this IEnumerable source)
|
||||
{
|
||||
foreach (object item in source)
|
||||
{
|
||||
yield return (TResult)Convert.ChangeType(item, typeof(TResult));
|
||||
}
|
||||
}
|
||||
|
||||
#endregion 强制转换类型
|
||||
|
||||
#region 转换为long
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为long,若转换失败,则返回0。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static long ParseToLong(this object obj)
|
||||
{
|
||||
try
|
||||
{
|
||||
return long.Parse(obj.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为long,若转换失败,则返回指定值。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <returns></returns>
|
||||
public static long ParseToLong(this string str, long defaultValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
return long.Parse(str);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion 转换为long
|
||||
|
||||
#region 转换为int
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为int,若转换失败,则返回0。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static int ParseToInt(this object str)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Convert.ToInt32(str);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为int,若转换失败,则返回指定值。不抛出异常。
|
||||
/// null返回默认值
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <returns></returns>
|
||||
public static int ParseToInt(this object str, int defaultValue)
|
||||
{
|
||||
if (str == null)
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
try
|
||||
{
|
||||
return Convert.ToInt32(str);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion 转换为int
|
||||
|
||||
#region 转换为short
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为short,若转换失败,则返回0。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static short ParseToShort(this object obj)
|
||||
{
|
||||
try
|
||||
{
|
||||
return short.Parse(obj.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为short,若转换失败,则返回指定值。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static short ParseToShort(this object str, short defaultValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
return short.Parse(str.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion 转换为short
|
||||
|
||||
#region 转换为demical
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为demical,若转换失败,则返回指定值。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal ParseToDecimal(this object str, decimal defaultValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
return decimal.Parse(str.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为demical,若转换失败,则返回0。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal ParseToDecimal(this object str)
|
||||
{
|
||||
try
|
||||
{
|
||||
return decimal.Parse(str.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion 转换为demical
|
||||
|
||||
#region 转化为bool
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为bool,若转换失败,则返回false。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static bool ParseToBool(this object str)
|
||||
{
|
||||
try
|
||||
{
|
||||
return bool.Parse(str.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为bool,若转换失败,则返回指定值。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static bool ParseToBool(this object str, bool result)
|
||||
{
|
||||
try
|
||||
{
|
||||
return bool.Parse(str.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion 转化为bool
|
||||
|
||||
#region 转换为float
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为float,若转换失败,则返回0。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static float ParseToFloat(this object str)
|
||||
{
|
||||
try
|
||||
{
|
||||
return float.Parse(str.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为float,若转换失败,则返回指定值。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static float ParseToFloat(this object str, float result)
|
||||
{
|
||||
try
|
||||
{
|
||||
return float.Parse(str.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion 转换为float
|
||||
|
||||
#region 转换为Guid
|
||||
|
||||
/// <summary>
|
||||
/// 将string转换为Guid,若转换失败,则返回Guid.Empty。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static Guid ParseToGuid(this string str)
|
||||
{
|
||||
try
|
||||
{
|
||||
return new Guid(str);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return Guid.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion 转换为Guid
|
||||
|
||||
#region 转换为DateTime
|
||||
|
||||
/// <summary>
|
||||
/// 将string转换为DateTime,若转换失败,则返回日期最小值。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime ParseToDateTime(this string str)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(str))
|
||||
{
|
||||
return DateTime.MinValue;
|
||||
}
|
||||
if (str.Contains("-") || str.Contains("/"))
|
||||
{
|
||||
return DateTime.Parse(str);
|
||||
}
|
||||
else
|
||||
{
|
||||
int length = str.Length;
|
||||
switch (length)
|
||||
{
|
||||
case 4:
|
||||
return DateTime.ParseExact(str, "yyyy", System.Globalization.CultureInfo.CurrentCulture);
|
||||
|
||||
case 6:
|
||||
return DateTime.ParseExact(str, "yyyyMM", System.Globalization.CultureInfo.CurrentCulture);
|
||||
|
||||
case 8:
|
||||
return DateTime.ParseExact(str, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
|
||||
|
||||
case 10:
|
||||
return DateTime.ParseExact(str, "yyyyMMddHH", System.Globalization.CultureInfo.CurrentCulture);
|
||||
|
||||
case 12:
|
||||
return DateTime.ParseExact(str, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture);
|
||||
|
||||
case 14:
|
||||
return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
|
||||
|
||||
default:
|
||||
return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return DateTime.MinValue;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将string转换为DateTime,若转换失败,则返回默认值。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <returns></returns>
|
||||
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);
|
||||
}
|
||||
else
|
||||
{
|
||||
int length = str.Length;
|
||||
switch (length)
|
||||
{
|
||||
case 4:
|
||||
return DateTime.ParseExact(str, "yyyy", System.Globalization.CultureInfo.CurrentCulture);
|
||||
|
||||
case 6:
|
||||
return DateTime.ParseExact(str, "yyyyMM", System.Globalization.CultureInfo.CurrentCulture);
|
||||
|
||||
case 8:
|
||||
return DateTime.ParseExact(str, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
|
||||
|
||||
case 10:
|
||||
return DateTime.ParseExact(str, "yyyyMMddHH", System.Globalization.CultureInfo.CurrentCulture);
|
||||
|
||||
case 12:
|
||||
return DateTime.ParseExact(str, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture);
|
||||
|
||||
case 14:
|
||||
return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
|
||||
|
||||
default:
|
||||
return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return defaultValue.GetValueOrDefault();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion 转换为DateTime
|
||||
|
||||
#region 转换为string
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为string,若转换失败,则返回""。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string ParseToString(this object obj)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
else
|
||||
{
|
||||
return obj.ToString();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
public static string ParseToStrings<T>(this object obj)
|
||||
{
|
||||
try
|
||||
{
|
||||
var list = obj as IEnumerable<T>;
|
||||
if (list != null)
|
||||
{
|
||||
return string.Join(",", list);
|
||||
}
|
||||
else
|
||||
{
|
||||
return obj.ToString();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion 转换为string
|
||||
|
||||
#region 转换为double
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为double,若转换失败,则返回0。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public static double ParseToDouble(this object obj)
|
||||
{
|
||||
try
|
||||
{
|
||||
return double.Parse(obj.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为double,若转换失败,则返回指定值。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <returns></returns>
|
||||
public static double ParseToDouble(this object str, double defaultValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
return double.Parse(str.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion 转换为double
|
||||
|
||||
/// <summary>
|
||||
/// 安全返回值
|
||||
/// </summary>
|
||||
/// <param name="value">可空值</param>
|
||||
public static T SafeValue<T>(this T? value) where T : struct
|
||||
{
|
||||
return value ?? default(T);
|
||||
}
|
||||
}
|
||||
}
|
||||
187
WaterCloud.Code/Extend/Ext.DateTime.cs
Normal file
187
WaterCloud.Code/Extend/Ext.DateTime.cs
Normal file
@@ -0,0 +1,187 @@
|
||||
/*******************************************************************************
|
||||
* Copyright © 2016 WaterCloud.Framework 版权所有
|
||||
* Author: WaterCloud
|
||||
* Description: WaterCloud快速开发平台
|
||||
* Website:
|
||||
*********************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace WaterCloud.Code
|
||||
{
|
||||
public static partial class Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取格式化字符串,带时分秒,格式:"yyyy-MM-dd HH:mm:ss"
|
||||
/// </summary>
|
||||
/// <param name="dateTime">日期</param>
|
||||
/// <param name="isRemoveSecond">是否移除秒</param>
|
||||
public static string ToDateTimeString(this DateTime dateTime, bool isRemoveSecond = false)
|
||||
{
|
||||
if (isRemoveSecond)
|
||||
return dateTime.ToString("yyyy-MM-dd HH:mm");
|
||||
return dateTime.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取格式化字符串,带时分秒,格式:"yyyy-MM-dd HH:mm:ss"
|
||||
/// </summary>
|
||||
/// <param name="dateTime">日期</param>
|
||||
/// <param name="isRemoveSecond">是否移除秒</param>
|
||||
public static string ToDateTimeString(this DateTime? dateTime, bool isRemoveSecond = false)
|
||||
{
|
||||
if (dateTime == null)
|
||||
return string.Empty;
|
||||
return ToDateTimeString(dateTime.Value, isRemoveSecond);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取格式化字符串,不带时分秒,格式:"yyyy-MM-dd"
|
||||
/// </summary>
|
||||
/// <param name="dateTime">日期</param>
|
||||
public static string ToDateString(this DateTime dateTime)
|
||||
{
|
||||
return dateTime.ToString("yyyy-MM-dd");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取格式化字符串,不带时分秒,格式:"yyyy-MM-dd"
|
||||
/// </summary>
|
||||
/// <param name="dateTime">日期</param>
|
||||
public static string ToDateString()
|
||||
{
|
||||
return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取格式化字符串,不带时分秒,格式:"yyyy-MM-dd"
|
||||
/// </summary>
|
||||
/// <param name="dateTime">日期</param>
|
||||
public static string ToDateString(this DateTime? dateTime)
|
||||
{
|
||||
if (dateTime == null)
|
||||
return string.Empty;
|
||||
return ToDateString(dateTime.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取格式化字符串,不带年月日,格式:"HH:mm:ss"
|
||||
/// </summary>
|
||||
/// <param name="dateTime">日期</param>
|
||||
public static string ToTimeString(this DateTime dateTime)
|
||||
{
|
||||
return dateTime.ToString("HH:mm:ss");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取格式化字符串,不带年月日,格式:"HH:mm:ss"
|
||||
/// </summary>
|
||||
/// <param name="dateTime">日期</param>
|
||||
public static string ToTimeString(this DateTime? dateTime)
|
||||
{
|
||||
if (dateTime == null)
|
||||
return string.Empty;
|
||||
return ToTimeString(dateTime.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取格式化字符串,带毫秒,格式:"yyyy-MM-dd HH:mm:ss.fff"
|
||||
/// </summary>
|
||||
/// <param name="dateTime">日期</param>
|
||||
public static string ToMillisecondString(this DateTime dateTime)
|
||||
{
|
||||
return dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取格式化字符串,带毫秒,格式:"yyyy-MM-dd HH:mm:ss.fff"
|
||||
/// </summary>
|
||||
/// <param name="dateTime">日期</param>
|
||||
public static string ToMillisecondString(this DateTime? dateTime)
|
||||
{
|
||||
if (dateTime == null)
|
||||
return string.Empty;
|
||||
return ToMillisecondString(dateTime.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取格式化字符串,不带时分秒,格式:"yyyy年MM月dd日"
|
||||
/// </summary>
|
||||
/// <param name="dateTime">日期</param>
|
||||
public static string ToChineseDateString(this DateTime dateTime)
|
||||
{
|
||||
return string.Format("{0}年{1}月{2}日", dateTime.Year, dateTime.Month, dateTime.Day);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取格式化字符串,不带时分秒,格式:"yyyy年MM月dd日"
|
||||
/// </summary>
|
||||
/// <param name="dateTime">日期</param>
|
||||
public static string ToChineseDateString(this DateTime? dateTime)
|
||||
{
|
||||
if (dateTime == null)
|
||||
return string.Empty;
|
||||
return ToChineseDateString(dateTime.SafeValue());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取格式化字符串,带时分秒,格式:"yyyy年MM月dd日 HH时mm分"
|
||||
/// </summary>
|
||||
/// <param name="dateTime">日期</param>
|
||||
/// <param name="isRemoveSecond">是否移除秒</param>
|
||||
public static string ToChineseDateTimeString(this DateTime dateTime, bool isRemoveSecond = false)
|
||||
{
|
||||
StringBuilder result = new StringBuilder();
|
||||
result.AppendFormat("{0}年{1}月{2}日", dateTime.Year, dateTime.Month, dateTime.Day);
|
||||
result.AppendFormat(" {0}时{1}分", dateTime.Hour, dateTime.Minute);
|
||||
if (isRemoveSecond == false)
|
||||
result.AppendFormat("{0}秒", dateTime.Second);
|
||||
return result.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取格式化字符串,带时分秒,格式:"yyyy年MM月dd日 HH时mm分"
|
||||
/// </summary>
|
||||
/// <param name="dateTime">日期</param>
|
||||
/// <param name="isRemoveSecond">是否移除秒</param>
|
||||
public static string ToChineseDateTimeString(this DateTime? dateTime, bool isRemoveSecond = false)
|
||||
{
|
||||
if (dateTime == null)
|
||||
return string.Empty;
|
||||
return ToChineseDateTimeString(dateTime.Value);
|
||||
}
|
||||
|
||||
#region 毫秒转天时分秒
|
||||
|
||||
/// <summary>
|
||||
/// 毫秒转天时分秒
|
||||
/// </summary>
|
||||
/// <param name="ms"></param>
|
||||
/// <returns></returns>
|
||||
public static string FormatTime(long ms)
|
||||
{
|
||||
int ss = 1000;
|
||||
int mi = ss * 60;
|
||||
int hh = mi * 60;
|
||||
int dd = hh * 24;
|
||||
|
||||
long day = ms / dd;
|
||||
long hour = (ms - day * dd) / hh;
|
||||
long minute = (ms - day * dd - hour * hh) / mi;
|
||||
long second = (ms - day * dd - hour * hh - minute * mi) / ss;
|
||||
long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
|
||||
|
||||
string sDay = day < 10 ? "0" + day : "" + day; //天
|
||||
string sHour = hour < 10 ? "0" + hour : "" + hour;//小时
|
||||
string sMinute = minute < 10 ? "0" + minute : "" + minute;//分钟
|
||||
string sSecond = second < 10 ? "0" + second : "" + second;//秒
|
||||
string sMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;//毫秒
|
||||
sMilliSecond = milliSecond < 100 ? "0" + sMilliSecond : "" + sMilliSecond;
|
||||
|
||||
return string.Format("{0} 天 {1} 小时 {2} 分 {3} 秒", sDay, sHour, sMinute, sSecond);
|
||||
}
|
||||
|
||||
#endregion 毫秒转天时分秒
|
||||
}
|
||||
}
|
||||
114
WaterCloud.Code/Extend/Ext.Enum.cs
Normal file
114
WaterCloud.Code/Extend/Ext.Enum.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace WaterCloud.Code
|
||||
{
|
||||
public static partial class Extensions
|
||||
{
|
||||
#region 枚举成员转成dictionary类型
|
||||
|
||||
/// <summary>
|
||||
/// 转成dictionary类型
|
||||
/// </summary>
|
||||
/// <param name="enumType"></param>
|
||||
/// <returns></returns>
|
||||
public static Dictionary<int, string> EnumToDictionary(this Type enumType)
|
||||
{
|
||||
Dictionary<int, string> dictionary = new Dictionary<int, string>();
|
||||
Type typeDescription = typeof(DescriptionAttribute);
|
||||
FieldInfo[] fields = enumType.GetFields();
|
||||
int sValue = 0;
|
||||
string sText = string.Empty;
|
||||
foreach (FieldInfo field in fields)
|
||||
{
|
||||
if (field.FieldType.IsEnum)
|
||||
{
|
||||
sValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null));
|
||||
object[] arr = field.GetCustomAttributes(typeDescription, true);
|
||||
if (arr.Length > 0)
|
||||
{
|
||||
DescriptionAttribute da = (DescriptionAttribute)arr[0];
|
||||
sText = da.Description;
|
||||
}
|
||||
else
|
||||
{
|
||||
sText = field.Name;
|
||||
}
|
||||
dictionary.Add(sValue, sText);
|
||||
}
|
||||
}
|
||||
return dictionary;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 枚举成员转成键值对Json字符串
|
||||
/// </summary>
|
||||
/// <param name="enumType"></param>
|
||||
/// <returns></returns>
|
||||
public static string EnumToDictionaryString(this Type enumType)
|
||||
{
|
||||
List<KeyValuePair<int, string>> dictionaryList = EnumToDictionary(enumType).ToList();
|
||||
var sJson = JsonConvert.SerializeObject(dictionaryList);
|
||||
return sJson;
|
||||
}
|
||||
|
||||
#endregion 枚举成员转成dictionary类型
|
||||
|
||||
#region 获取枚举的描述
|
||||
|
||||
/// <summary>
|
||||
/// 获取枚举值对应的描述
|
||||
/// </summary>
|
||||
/// <param name="enumType"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetDescription(this System.Enum enumType)
|
||||
{
|
||||
FieldInfo EnumInfo = enumType.GetType().GetField(enumType.ToString());
|
||||
if (EnumInfo != null)
|
||||
{
|
||||
DescriptionAttribute[] EnumAttributes = (DescriptionAttribute[])EnumInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
|
||||
if (EnumAttributes.Length > 0)
|
||||
{
|
||||
return EnumAttributes[0].Description;
|
||||
}
|
||||
}
|
||||
return enumType.ToString();
|
||||
}
|
||||
|
||||
#endregion 获取枚举的描述
|
||||
|
||||
#region 根据值获取枚举的描述
|
||||
|
||||
public static string GetDescriptionByEnum<T>(this object obj)
|
||||
{
|
||||
var tEnum = System.Enum.Parse(typeof(T), obj.ParseToString()) as System.Enum;
|
||||
var description = tEnum.GetDescription();
|
||||
return description;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 枚举
|
||||
/// </summary>
|
||||
/// <param name="enumType"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToDescription(this Enum enumType)
|
||||
{
|
||||
if (enumType == null)
|
||||
return "";
|
||||
|
||||
System.Reflection.FieldInfo fieldInfo = enumType.GetType().GetField(enumType.ToString());
|
||||
|
||||
object[] attribArray = fieldInfo.GetCustomAttributes(false);
|
||||
if (attribArray.Length == 0)
|
||||
return enumType.ToString();
|
||||
else
|
||||
return (attribArray[0] as DescriptionAttribute).Description;
|
||||
}
|
||||
|
||||
#endregion 根据值获取枚举的描述
|
||||
}
|
||||
}
|
||||
14
WaterCloud.Code/Extend/Ext.Exception.cs
Normal file
14
WaterCloud.Code/Extend/Ext.Exception.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
|
||||
namespace WaterCloud.Code
|
||||
{
|
||||
public static partial class Extensions
|
||||
{
|
||||
public static Exception GetOriginalException(this Exception ex)
|
||||
{
|
||||
if (ex.InnerException == null) return ex;
|
||||
|
||||
return ex.InnerException.GetOriginalException();
|
||||
}
|
||||
}
|
||||
}
|
||||
156
WaterCloud.Code/Extend/Ext.Format.cs
Normal file
156
WaterCloud.Code/Extend/Ext.Format.cs
Normal file
@@ -0,0 +1,156 @@
|
||||
/*******************************************************************************
|
||||
* Copyright © 2016 WaterCloud.Framework 版权所有
|
||||
* Author: WaterCloud
|
||||
* Description: WaterCloud快速开发平台
|
||||
* Website:
|
||||
*********************************************************************************/
|
||||
|
||||
namespace WaterCloud.Code
|
||||
{
|
||||
public static partial class Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取描述
|
||||
/// </summary>
|
||||
/// <param name="value">布尔值</param>
|
||||
public static string Description(this bool value)
|
||||
{
|
||||
return value ? "是" : "否";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取描述
|
||||
/// </summary>
|
||||
/// <param name="value">布尔值</param>
|
||||
public static string Description(this bool? value)
|
||||
{
|
||||
return value == null ? "" : Description(value.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取格式化字符串
|
||||
/// </summary>
|
||||
/// <param name="number">数值</param>
|
||||
/// <param name="defaultValue">空值显示的默认文本</param>
|
||||
public static string Format(this int number, string defaultValue = "")
|
||||
{
|
||||
if (number == 0)
|
||||
return defaultValue;
|
||||
return number.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取格式化字符串
|
||||
/// </summary>
|
||||
/// <param name="number">数值</param>
|
||||
/// <param name="defaultValue">空值显示的默认文本</param>
|
||||
public static string Format(this int? number, string defaultValue = "")
|
||||
{
|
||||
return Format(number.SafeValue(), defaultValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取格式化字符串
|
||||
/// </summary>
|
||||
/// <param name="number">数值</param>
|
||||
/// <param name="defaultValue">空值显示的默认文本</param>
|
||||
public static string Format(this decimal number, string defaultValue = "")
|
||||
{
|
||||
if (number == 0)
|
||||
return defaultValue;
|
||||
return string.Format("{0:0.##}", number);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取格式化字符串
|
||||
/// </summary>
|
||||
/// <param name="number">数值</param>
|
||||
/// <param name="defaultValue">空值显示的默认文本</param>
|
||||
public static string Format(this decimal? number, string defaultValue = "")
|
||||
{
|
||||
return Format(number.SafeValue(), defaultValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取格式化字符串
|
||||
/// </summary>
|
||||
/// <param name="number">数值</param>
|
||||
/// <param name="defaultValue">空值显示的默认文本</param>
|
||||
public static string Format(this double number, string defaultValue = "")
|
||||
{
|
||||
if (number == 0)
|
||||
return defaultValue;
|
||||
return string.Format("{0:0.##}", number);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取格式化字符串
|
||||
/// </summary>
|
||||
/// <param name="number">数值</param>
|
||||
/// <param name="defaultValue">空值显示的默认文本</param>
|
||||
public static string Format(this double? number, string defaultValue = "")
|
||||
{
|
||||
return Format(number.SafeValue(), defaultValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取格式化字符串,带¥
|
||||
/// </summary>
|
||||
/// <param name="number">数值</param>
|
||||
public static string FormatRmb(this decimal number)
|
||||
{
|
||||
if (number == 0)
|
||||
return "¥0";
|
||||
return string.Format("¥{0:0.##}", number);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取格式化字符串,带¥
|
||||
/// </summary>
|
||||
/// <param name="number">数值</param>
|
||||
public static string FormatRmb(this decimal? number)
|
||||
{
|
||||
return FormatRmb(number.SafeValue());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取格式化字符串,带%
|
||||
/// </summary>
|
||||
/// <param name="number">数值</param>
|
||||
public static string FormatPercent(this decimal number)
|
||||
{
|
||||
if (number == 0)
|
||||
return string.Empty;
|
||||
return string.Format("{0:0.##}%", number);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取格式化字符串,带%
|
||||
/// </summary>
|
||||
/// <param name="number">数值</param>
|
||||
public static string FormatPercent(this decimal? number)
|
||||
{
|
||||
return FormatPercent(number.SafeValue());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取格式化字符串,带%
|
||||
/// </summary>
|
||||
/// <param name="number">数值</param>
|
||||
public static string FormatPercent(this double number)
|
||||
{
|
||||
if (number == 0)
|
||||
return string.Empty;
|
||||
return string.Format("{0:0.##}%", number);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取格式化字符串,带%
|
||||
/// </summary>
|
||||
/// <param name="number">数值</param>
|
||||
public static string FormatPercent(this double? number)
|
||||
{
|
||||
return FormatPercent(number.SafeValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
211
WaterCloud.Code/Extend/Ext.Linq.cs
Normal file
211
WaterCloud.Code/Extend/Ext.Linq.cs
Normal file
@@ -0,0 +1,211 @@
|
||||
/*******************************************************************************
|
||||
* Copyright © 2016 WaterCloud.Framework 版权所有
|
||||
* Author: WaterCloud
|
||||
* Description: WaterCloud快速开发平台
|
||||
* Website:
|
||||
*********************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
|
||||
namespace WaterCloud.Code
|
||||
{
|
||||
public static partial class ExtLinq
|
||||
{
|
||||
public static Expression Property(this Expression expression, string propertyName)
|
||||
{
|
||||
return Expression.Property(expression, propertyName);
|
||||
}
|
||||
|
||||
public static Expression AndAlso(this Expression left, Expression right)
|
||||
{
|
||||
return Expression.AndAlso(left, right);
|
||||
}
|
||||
|
||||
public static Expression OrElse(this Expression left, Expression right)
|
||||
{
|
||||
return Expression.OrElse(left, right);
|
||||
}
|
||||
|
||||
public static Expression Call(this Expression instance, string methodName, params Expression[] arguments)
|
||||
{
|
||||
return Expression.Call(instance, instance.Type.GetMethod(methodName), arguments);
|
||||
}
|
||||
|
||||
public static Expression GreaterThan(this Expression left, Expression right)
|
||||
{
|
||||
return Expression.GreaterThan(left, right);
|
||||
}
|
||||
|
||||
public static Expression<T> ToLambda<T>(this Expression body, params ParameterExpression[] parameters)
|
||||
{
|
||||
return Expression.Lambda<T>(body, parameters);
|
||||
}
|
||||
|
||||
public static Expression<Func<T, bool>> True<T>()
|
||||
{ return param => true; }
|
||||
|
||||
public static Expression<Func<T, bool>> False<T>()
|
||||
{ return param => false; }
|
||||
|
||||
public static Expression<Func<T, bool>> AndAlso<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
|
||||
{
|
||||
return first.Compose(second, Expression.AndAlso);
|
||||
}
|
||||
|
||||
public static Expression<Func<T, bool>> OrElse<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
|
||||
{
|
||||
return first.Compose(second, Expression.OrElse);
|
||||
}
|
||||
|
||||
public static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
|
||||
{
|
||||
var map = first.Parameters
|
||||
.Select((f, i) => new { f, s = second.Parameters[i] })
|
||||
.ToDictionary(p => p.s, p => p.f);
|
||||
var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);
|
||||
return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
|
||||
}
|
||||
|
||||
private class ParameterRebinder : ExpressionVisitor
|
||||
{
|
||||
private readonly Dictionary<ParameterExpression, ParameterExpression> map;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ParameterRebinder"/> class.
|
||||
/// </summary>
|
||||
/// <param name="map">The map.</param>
|
||||
private ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map)
|
||||
{
|
||||
this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replaces the parameters.
|
||||
/// </summary>
|
||||
/// <param name="map">The map.</param>
|
||||
/// <param name="exp">The exp.</param>
|
||||
/// <returns>Expression</returns>
|
||||
public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp)
|
||||
{
|
||||
return new ParameterRebinder(map).Visit(exp);
|
||||
}
|
||||
|
||||
protected override Expression VisitParameter(ParameterExpression p)
|
||||
{
|
||||
ParameterExpression replacement;
|
||||
|
||||
if (map.TryGetValue(p, out replacement))
|
||||
{
|
||||
p = replacement;
|
||||
}
|
||||
return base.VisitParameter(p);
|
||||
}
|
||||
}
|
||||
|
||||
public static ParameterExpression CreateLambdaParam<T>(string name)
|
||||
{
|
||||
return Expression.Parameter(typeof(T), name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建完整的lambda
|
||||
/// </summary>
|
||||
public static LambdaExpression GenerateLambda(this ParameterExpression param, Expression body)
|
||||
{
|
||||
//c=>c.XXX=="XXX"
|
||||
return Expression.Lambda(body, param);
|
||||
}
|
||||
|
||||
public static Expression<Func<T, bool>> GenerateTypeLambda<T>(this ParameterExpression param, Expression body)
|
||||
{
|
||||
return (Expression<Func<T, bool>>)(param.GenerateLambda(body));
|
||||
}
|
||||
|
||||
public static Expression Or(this Expression expression, Expression expressionRight)
|
||||
{
|
||||
return Expression.Or(expression, expressionRight);
|
||||
}
|
||||
|
||||
public static Expression And(this Expression expression, Expression expressionRight)
|
||||
{
|
||||
return Expression.And(expression, expressionRight);
|
||||
}
|
||||
|
||||
public static IOrderedQueryable<TEntity> SortBy<TEntity>(this IQueryable<TEntity> query, Expression<Func<TEntity, dynamic>> sortPredicate)
|
||||
where TEntity : class, new()
|
||||
{
|
||||
return InvokeSortBy(query, sortPredicate, SortOrder.Ascending);
|
||||
}
|
||||
|
||||
public static IOrderedQueryable<TEntity> SortByDescending<TEntity>(this IQueryable<TEntity> query, Expression<Func<TEntity, dynamic>> sortPredicate)
|
||||
where TEntity : class, new()
|
||||
{
|
||||
return InvokeSortBy(query, sortPredicate, SortOrder.Descending);
|
||||
}
|
||||
|
||||
private static IOrderedQueryable<TEntity> InvokeSortBy<TEntity>(IQueryable<TEntity> query,
|
||||
Expression<Func<TEntity, dynamic>> sortPredicate, SortOrder sortOrder)
|
||||
where TEntity : class, new()
|
||||
{
|
||||
var param = sortPredicate.Parameters[0];
|
||||
string propertyName = null;
|
||||
Type propertyType = null;
|
||||
Expression bodyExpression = null;
|
||||
if (sortPredicate.Body is UnaryExpression)
|
||||
{
|
||||
var unaryExpression = sortPredicate.Body as UnaryExpression;
|
||||
bodyExpression = unaryExpression.Operand;
|
||||
}
|
||||
else if (sortPredicate.Body is MemberExpression)
|
||||
{
|
||||
bodyExpression = sortPredicate.Body;
|
||||
}
|
||||
else
|
||||
throw new ArgumentException(@"The body of the sort predicate expression should be
|
||||
either UnaryExpression or MemberExpression.", "sortPredicate");
|
||||
var memberExpression = (MemberExpression)bodyExpression;
|
||||
propertyName = memberExpression.Member.Name;
|
||||
if (memberExpression.Member.MemberType == MemberTypes.Property)
|
||||
{
|
||||
var propertyInfo = memberExpression.Member as PropertyInfo;
|
||||
if (propertyInfo != null) propertyType = propertyInfo.PropertyType;
|
||||
}
|
||||
else
|
||||
throw new InvalidOperationException(@"Cannot evaluate the type of property since the member expression
|
||||
represented by the sort predicate expression does not contain a PropertyInfo object.");
|
||||
|
||||
var funcType = typeof(Func<,>).MakeGenericType(typeof(TEntity), propertyType);
|
||||
var convertedExpression = Expression.Lambda(funcType,
|
||||
Expression.Convert(Expression.Property(param, propertyName), propertyType), param);
|
||||
|
||||
var sortingMethods = typeof(Queryable).GetMethods(BindingFlags.Public | BindingFlags.Static);
|
||||
var sortingMethodName = GetSortingMethodName(sortOrder);
|
||||
var sortingMethod = sortingMethods.First(sm => sm.Name == sortingMethodName &&
|
||||
sm.GetParameters().Length == 2);
|
||||
return (IOrderedQueryable<TEntity>)sortingMethod
|
||||
.MakeGenericMethod(typeof(TEntity), propertyType)
|
||||
.Invoke(null, new object[] { query, convertedExpression });
|
||||
}
|
||||
|
||||
private static string GetSortingMethodName(SortOrder sortOrder)
|
||||
{
|
||||
switch (sortOrder)
|
||||
{
|
||||
case SortOrder.Ascending:
|
||||
return "OrderBy";
|
||||
|
||||
case SortOrder.Descending:
|
||||
return "OrderByDescending";
|
||||
|
||||
default:
|
||||
throw new ArgumentException("Sort Order must be specified as either Ascending or Descending.",
|
||||
"sortOrder");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
103
WaterCloud.Code/Extend/Ext.List.cs
Normal file
103
WaterCloud.Code/Extend/Ext.List.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
/*******************************************************************************
|
||||
* Copyright © 2016 WaterCloud.Framework 版权所有
|
||||
* Author: WaterCloud
|
||||
* Description: WaterCloud快速开发平台
|
||||
* Website:
|
||||
*********************************************************************************/
|
||||
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace WaterCloud.Code
|
||||
{
|
||||
public static partial class Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取表里某页的数据
|
||||
/// </summary>
|
||||
/// <param name="data">表数据</param>
|
||||
/// <param name="pageIndex">当前页</param>
|
||||
/// <param name="pageSize">分页大小</param>
|
||||
/// <param name="allPage">返回总页数</param>
|
||||
/// <returns>返回当页表数据</returns>
|
||||
public static List<T> GetPage<T>(this List<T> data, int pageIndex, int pageSize, out int allPage)
|
||||
{
|
||||
allPage = 1;
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// IList转成List<T>
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="list"></param>
|
||||
/// <returns></returns>
|
||||
public static List<T> IListToList<T>(IList list)
|
||||
{
|
||||
T[] array = new T[list.Count];
|
||||
list.CopyTo(array, 0);
|
||||
return new List<T>(array);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 去除空元素
|
||||
/// </summary>
|
||||
public static List<string> removeNull(List<string> oldList)
|
||||
{
|
||||
// 临时集合
|
||||
List<string> listTemp = new List<string>();
|
||||
foreach (var item in oldList)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(item))
|
||||
{
|
||||
listTemp.Add(item);
|
||||
}
|
||||
}
|
||||
return listTemp;
|
||||
}
|
||||
}
|
||||
|
||||
public class ExtList<T> : IEqualityComparer<T> where T : class, new()
|
||||
{
|
||||
private string[] comparintFiledName = { };
|
||||
|
||||
public ExtList()
|
||||
{ }
|
||||
|
||||
public ExtList(params string[] comparintFiledName)
|
||||
{
|
||||
this.comparintFiledName = comparintFiledName;
|
||||
}
|
||||
|
||||
bool IEqualityComparer<T>.Equals(T x, T y)
|
||||
{
|
||||
if (x == null && y == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (comparintFiledName.Length == 0)
|
||||
{
|
||||
return x.Equals(y);
|
||||
}
|
||||
bool result = true;
|
||||
var typeX = x.GetType();//获取类型
|
||||
var typeY = y.GetType();
|
||||
foreach (var filedName in comparintFiledName)
|
||||
{
|
||||
var xPropertyInfo = (from p in typeX.GetProperties() where p.Name.Equals(filedName) select p).FirstOrDefault();
|
||||
var yPropertyInfo = (from p in typeY.GetProperties() where p.Name.Equals(filedName) select p).FirstOrDefault();
|
||||
|
||||
result = result
|
||||
&& xPropertyInfo != null && yPropertyInfo != null
|
||||
&& xPropertyInfo.GetValue(x, null).ToString().Equals(yPropertyInfo.GetValue(y, null));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int IEqualityComparer<T>.GetHashCode(T obj)
|
||||
{
|
||||
return obj.ToString().GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
59
WaterCloud.Code/Extend/Ext.Mapper.cs
Normal file
59
WaterCloud.Code/Extend/Ext.Mapper.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using AutoMapper;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace WaterCloud.Code
|
||||
{
|
||||
public static partial class Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 类型映射
|
||||
/// </summary>
|
||||
public static T MapTo<T>(this object obj)
|
||||
{
|
||||
if (obj == null) return default(T);
|
||||
|
||||
var config = new MapperConfiguration(cfg => cfg.CreateMap(obj.GetType(), typeof(T)));
|
||||
var mapper = config.CreateMapper();
|
||||
return mapper.Map<T>(obj);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 集合列表类型映射
|
||||
/// </summary>
|
||||
public static List<TDestination> MapToList<TDestination>(this IEnumerable source)
|
||||
{
|
||||
Type sourceType = source.GetType().GetGenericArguments()[0]; //获取枚举的成员类型
|
||||
var config = new MapperConfiguration(cfg => cfg.CreateMap(sourceType, typeof(TDestination)));
|
||||
var mapper = config.CreateMapper();
|
||||
|
||||
return mapper.Map<List<TDestination>>(source);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 集合列表类型映射
|
||||
/// </summary>
|
||||
public static List<TDestination> MapToList<TSource, TDestination>(this IEnumerable<TSource> source)
|
||||
{
|
||||
var config = new MapperConfiguration(cfg => cfg.CreateMap(typeof(TSource), typeof(TDestination)));
|
||||
var mapper = config.CreateMapper();
|
||||
|
||||
return mapper.Map<List<TDestination>>(source);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 类型映射
|
||||
/// </summary>
|
||||
public static TDestination MapTo<TSource, TDestination>(this TSource source, TDestination destination)
|
||||
where TSource : class
|
||||
where TDestination : class
|
||||
{
|
||||
if (source == null) return destination;
|
||||
|
||||
var config = new MapperConfiguration(cfg => cfg.CreateMap(typeof(TSource), typeof(TDestination)));
|
||||
var mapper = config.CreateMapper();
|
||||
return mapper.Map<TDestination>(source);
|
||||
}
|
||||
}
|
||||
}
|
||||
96
WaterCloud.Code/Extend/Ext.Number.cs
Normal file
96
WaterCloud.Code/Extend/Ext.Number.cs
Normal file
@@ -0,0 +1,96 @@
|
||||
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace WaterCloud.Code
|
||||
{
|
||||
/// <summary>
|
||||
/// 数值扩展类
|
||||
/// </summary>
|
||||
public static partial class Extensions
|
||||
{
|
||||
#region 进制转换
|
||||
/// <summary>
|
||||
/// 10进制转换到2-36进制
|
||||
/// </summary>
|
||||
/// <param name="this">10进制数字</param>
|
||||
/// <param name="radix">进制,范围2-36</param>
|
||||
/// <param name="digits">编码取值规则,最大转换位数不能大于该字符串的长度</param>
|
||||
/// <returns></returns>
|
||||
public static string ToBase(this long @this, int radix, string digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||
{
|
||||
const int BitsInLong = 64;
|
||||
|
||||
if (radix < 2 || radix > digits.Length)
|
||||
throw new ArgumentException("The radix must be >= 2 and <= " + digits.Length.ToString());
|
||||
|
||||
if (@this == 0)
|
||||
return "0";
|
||||
|
||||
var index = BitsInLong - 1;
|
||||
var currentNumber = Math.Abs(@this);
|
||||
var charArray = new char[BitsInLong];
|
||||
|
||||
while (currentNumber != 0)
|
||||
{
|
||||
var remainder = (int)(currentNumber % radix);
|
||||
charArray[index--] = digits[remainder];
|
||||
currentNumber /= radix;
|
||||
}
|
||||
|
||||
var result = new string(charArray, index + 1, BitsInLong - index - 1);
|
||||
if (@this < 0)
|
||||
{
|
||||
result = "-" + result;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// byte转16进制
|
||||
/// </summary>
|
||||
/// <param name="this"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToHex(this byte @this) => Convert.ToString(@this, 16);
|
||||
|
||||
/// <summary>
|
||||
/// 2进制转16进制
|
||||
/// </summary>
|
||||
/// <param name="this"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToHex(this string @this) => Convert.ToString(Convert.ToInt64(@this, 2), 16);
|
||||
|
||||
/// <summary>
|
||||
/// 16进制转2进制
|
||||
/// </summary>
|
||||
/// <param name="this"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToBinary(this string @this) => Convert.ToString(Convert.ToInt64(@this, 16), 2);
|
||||
|
||||
/// <summary>
|
||||
/// 2进制/16进制转8进制
|
||||
/// </summary>
|
||||
/// <param name="this"></param>
|
||||
/// <param name="fromBase">2或者16,表示2进制或者16进制;</param>
|
||||
/// <returns></returns>
|
||||
public static string ToOctal(this string @this, int fromBase) => Convert.ToString(Convert.ToInt64(@this, fromBase), 8);
|
||||
|
||||
/// <summary>
|
||||
/// 2进制/16进制转10进制
|
||||
/// </summary>
|
||||
/// <param name="this"></param>
|
||||
/// <param name="fromBase">2或者16,表示2进制或者16进制;</param>
|
||||
/// <returns></returns>
|
||||
public static string ToDecimalism(this string @this, int fromBase)
|
||||
{
|
||||
if (fromBase == 16)
|
||||
return Convert.ToInt32(@this, 16).ToString();
|
||||
else
|
||||
return Convert.ToString(Convert.ToInt64(@this, 2), 10);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
37
WaterCloud.Code/Extend/Ext.String.cs
Normal file
37
WaterCloud.Code/Extend/Ext.String.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
|
||||
namespace WaterCloud.Code
|
||||
{
|
||||
/// <summary>
|
||||
/// string扩展类
|
||||
/// </summary>
|
||||
public static partial class Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 从分隔符开始向尾部截取字符串
|
||||
/// </summary>
|
||||
/// <param name="this">源字符串</param>
|
||||
/// <param name="separator">分隔符</param>
|
||||
/// <param name="lastIndexOf">true:从最后一个匹配的分隔符开始截取,false:从第一个匹配的分隔符开始截取,默认:true</param>
|
||||
/// <returns>string</returns>
|
||||
public static string Substring(this string @this, string separator, bool lastIndexOf = true)
|
||||
{
|
||||
var startIndex = (lastIndexOf ?
|
||||
@this.LastIndexOf(separator, StringComparison.OrdinalIgnoreCase) :
|
||||
@this.IndexOf(separator, StringComparison.OrdinalIgnoreCase)) +
|
||||
separator.Length;
|
||||
|
||||
var length = @this.Length - startIndex;
|
||||
return @this.Substring(startIndex, length);
|
||||
}
|
||||
#region 字符串截取第一个
|
||||
public static string ReplaceFrist(this string str, string oldChar, string newChar)
|
||||
{
|
||||
int idx = str.IndexOf(oldChar);
|
||||
str = str.Remove(idx, oldChar.Length);
|
||||
str = str.Insert(idx, newChar);
|
||||
return str;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
70
WaterCloud.Code/Extend/Ext.Table.cs
Normal file
70
WaterCloud.Code/Extend/Ext.Table.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
/*******************************************************************************
|
||||
* Copyright © 2016 WaterCloud.Framework 版权所有
|
||||
* Author: WaterCloud
|
||||
* Description: WaterCloud快速开发平台
|
||||
* Website:
|
||||
*********************************************************************************/
|
||||
|
||||
using System.Data;
|
||||
|
||||
namespace WaterCloud.Code
|
||||
{
|
||||
public static partial class Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取表里某页的数据
|
||||
/// </summary>
|
||||
/// <param name="data">表数据</param>
|
||||
/// <param name="pageIndex">当前页</param>
|
||||
/// <param name="pageSize">分页大小</param>
|
||||
/// <param name="allPage">返回总页数</param>
|
||||
/// <returns>返回当页表数据</returns>
|
||||
public static DataTable GetPage(this DataTable data, int pageIndex, int pageSize, out int allPage)
|
||||
{
|
||||
allPage = data.Rows.Count / pageSize;
|
||||
allPage += data.Rows.Count % pageSize == 0 ? 0 : 1;
|
||||
DataTable Ntable = data.Clone();
|
||||
int startIndex = pageIndex * pageSize;
|
||||
int endIndex = startIndex + pageSize > data.Rows.Count ? data.Rows.Count : startIndex + pageSize;
|
||||
if (startIndex < endIndex)
|
||||
for (int i = startIndex; i < endIndex; i++)
|
||||
{
|
||||
Ntable.ImportRow(data.Rows[i]);
|
||||
}
|
||||
return Ntable;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据字段过滤表的内容
|
||||
/// </summary>
|
||||
/// <param name="data">表数据</param>
|
||||
/// <param name="condition">条件</param>
|
||||
/// <returns></returns>
|
||||
///
|
||||
public static DataTable GetDataFilter(DataTable data, string condition)
|
||||
{
|
||||
if (data != null && data.Rows.Count > 0)
|
||||
{
|
||||
if (condition.Trim() == "")
|
||||
{
|
||||
return data;
|
||||
}
|
||||
else
|
||||
{
|
||||
DataTable newdt = new DataTable();
|
||||
newdt = data.Clone();
|
||||
DataRow[] dr = data.Select(condition);
|
||||
for (int i = 0; i < dr.Length; i++)
|
||||
{
|
||||
newdt.ImportRow((DataRow)dr[i]);
|
||||
}
|
||||
return newdt;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
30
WaterCloud.Code/Extend/Ext.Validate.cs
Normal file
30
WaterCloud.Code/Extend/Ext.Validate.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
|
||||
namespace WaterCloud.Code
|
||||
{
|
||||
public static partial class Extensions
|
||||
{
|
||||
public static bool IsNullOrZero(this object value)
|
||||
{
|
||||
if (value == null || value.ParseToString().Trim() == "0")
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsAjaxRequest(this HttpRequest request)
|
||||
{
|
||||
if (request == null)
|
||||
throw new ArgumentNullException("request");
|
||||
|
||||
if (request.Headers != null)
|
||||
return request.Headers["X-Requested-With"] == "XMLHttpRequest";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user