public static class ExtendHelper
{
/// <summary>
/// 檢查目前字元串是否符合某種格式
/// </summary>
/// <param name="thisValue"></param>
/// <param name="pattern"></param>
/// <returns></returns>
public static bool IsMatch(this string thisValue, string regexPattern)
{
return Regex.IsMatch(thisValue, regexPattern);
}
/// <summary>
/// 是否是有效的Email位址
/// </summary>
/// <param name="thisValue"></param>
/// <returns></returns>
public static bool IsEmail(this string thisValue)
{
return Regex.IsMatch(thisValue, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
}
/// <summary>
/// 如果為Null,則轉換成T類型的值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="thisValue"></param>
/// <param name="val"></param>
/// <returns></returns>
public static T IfNullThen<T>(this object thisValue, T val)
{
return thisValue == null ? val : (T)thisValue;
}
#region IsIn IsNotIn
/// <summary>
/// 是否在指定的字元串集合中,相當于SQL的In操作符
/// </summary>
/// <param name="thisValue"></param>
/// <param name="stringCollect"></param>
/// <returns></returns>
public static bool IsIn(this string thisValue, params string[] stringCollect)
{
if (stringCollect == null || stringCollect.Length <= 0)
return false;
return Array.IndexOf<string>(stringCollect, thisValue) > -1;
}
/// <summary>
/// 目前對象是否在某集合中
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="thisValue"></param>
/// <param name="collect"></param>
/// <param name="equalLogic">判斷相等的邏輯</param>
/// <returns></returns>
public static bool IsIn<T>(this T thisValue, List<T> collect, Func<T, T, bool> equalLogic)
{
if (collect == null || collect.Count <= 0)
return false;
bool result = false;
foreach (T item in collect)
{
if (equalLogic(item, thisValue))
{
result = true;
break;
}
}
return result;
}
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="thisValue"></param>
/// <param name="collect"></param>
/// <param name="equalLogic">判斷相等的邏輯</param>
/// <returns></returns>
public static bool IsNotIn<T>(this T thisValue, List<T> collect, Func<T, T, bool> equalLogic)
{
return !thisValue.IsIn(collect, equalLogic);
}
#endregion
/// <summary>
/// 是否以 startBy 參數開頭
/// </summary>
/// <param name="thisValue"></param>
/// <param name="startWith"></param>
/// <returns></returns>
public static bool StartBy(this string thisValue, params string[] startBy)
{
foreach (string item in startBy)
{
if (thisValue.StartsWith(item))
return true;
}
return false;
}
/// <summary>
/// 是否以 startBy 參數開頭
/// </summary>
/// <param name="thisValue"></param>
/// <param name="startBy"></param>
/// <param name="splitSign">startBy 的分隔符</param>
/// <returns></returns>
public static bool StartBy(this string thisValue, string startBy, char splitSign)
{
string[] starts = startBy.Split(splitSign);
if (starts != null && starts.Length > 0)
foreach (string item in starts)
{
if (thisValue.StartsWith(item))
return true;
}
return false;
}
/// <summary>
/// 根據指定的正規表達式進行替換
/// </summary>
/// <param name="thisValue"></param>
/// <param name="regexPattern"></param>
/// <param name="replacement"></param>
/// <returns></returns>
public static string ReplaceString(this string thisValue, string regexPattern, string replacement)
{
if (!string.IsNullOrEmpty(thisValue))
{
return Regex.Replace(thisValue, regexPattern, replacement, RegexOptions.IgnoreCase);
}
return string.Empty;
}
/// <summary>
/// 根據隻等的正規表達式擷取字元串
/// </summary>
/// <param name="thisValue"></param>
/// <param name="regexPattern"></param>
/// <returns></returns>
public static string[] GetSubString(this string thisValue, string regexPattern)
{
string[] result = null;
MatchCollection collection = Regex.Matches(thisValue, regexPattern, RegexOptions.IgnoreCase);
if (collection != null && collection.Count > 0)
{
result = new string[collection.Count];
for (int i = 0; i < collection.Count; i++)
{
result[i] = collection[i].Value;
}
}
return result;
}
/// <summary>
/// 從結果集中排除另一個結果集
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="thisList"></param>
/// <param name="rejectList"></param>
/// <param name="removeLogic">排除的邏輯</param>
/// <returns></returns>
public static List<T> Reject<T>(this List<T> thisList, List<T> rejectList, Func<T, T, bool> removeLogic)
{
foreach (var item in rejectList)
{
for (int i = 0; i < thisList.Count; i++)
{
T t = thisList[i];
if (removeLogic(t, item)) thisList.Remove(t);
}
}
return thisList;
}
/// <summary>
/// 是在某某時間之間,包含from和to
/// </summary>
/// <param name="thisValue"></param>
/// <param name="from"></param>
/// <param name="to"></param>
/// <returns></returns>
public static bool Between(this DateTime thisValue, DateTime from, DateTime to)
{
return thisValue >= from && thisValue <= to;
}
#region Transform
/// <summary>
/// 從一個對象Transform到另外一個對象
/// </summary>
/// <typeparam name="S"></typeparam>
/// <typeparam name="R"></typeparam>
/// <param name="source"></param>
/// <param name="RfieldNameFunc">source對象屬性名對應的Return對象的屬性名,預設是source對象的屬性名對應return對象的屬性名</param>
/// <returns></returns>
public static R Transform<S, R>(this S source, Func<string, string> RfieldNameFunc = null) where R : new()
{
R result = new R();
result.SetProperties<S, R>(source, RfieldNameFunc);
return result;
}
/// <summary>
///
/// </summary>
/// <typeparam name="S"></typeparam>
/// <typeparam name="R"></typeparam>
/// <param name="sourceList"></param>
/// <param name="RfieldNameFunc">source對象屬性名對應的Return對象的屬性名,預設是source對象的屬性名對應return對象的屬性名</param>
/// <returns></returns>
public static List<R> Transform<S, R>(this IEnumerable<S> sourceList, Func<string, string> RfieldNameFunc = null) where R : new()
{
List<R> resultList = new List<R>();
foreach (S item in sourceList)
{
resultList.Add(item.Transform<S, R>(RfieldNameFunc));
}
return resultList;
}
/// <summary>
/// 設定屬性
/// </summary>
/// <typeparam name="S"></typeparam>
/// <typeparam name="R"></typeparam>
/// <param name="thisValue"></param>
/// <param name="source"></param>
/// <param name="RfieldNameFunc">要設定屬性的名字,預設是source對象的屬性名對應return對象的屬性名</param>
public static void SetProperties<S, R>(this R thisValue, S source, Func<string, string> RfieldNameFunc = null)
{
PropertyInfo[] sourceFields = typeof(S).GetProperties();
#region Set Result Property
if (sourceFields != null)
{
foreach (PropertyInfo sourceProperty in sourceFields)
{
PropertyInfo resultProperty = null;
foreach (var item in typeof(R).GetProperties())
{
string rfieldName = RfieldNameFunc != null ? RfieldNameFunc(sourceProperty.Name).ToLower() : sourceProperty.Name.ToLower();
if (item.Name.ToLower() == rfieldName)
{
resultProperty = item;
break;
}
}
if (resultProperty != null && resultProperty.CanWrite)
{
object val = sourceProperty.GetValue(source, null);
if (resultProperty.PropertyType.IsGenericType && resultProperty.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
/* 如果目标類型是可空類型
* 1. source.value != null, 則用基類型取值,然後給目标類型指派
* 2. source.value = null,則目标類型用預設值null
*/
if (val != null)
{
Type baseType = resultProperty.PropertyType.GetGenericArguments()[0];
resultProperty.SetValue(thisValue, Convert.ChangeType(val, baseType, null), null);
}
}
else
{
/* 目标類型不是可空類型
* 1. source.value = null,則取source.value的預設值給目标類型指派
* 2. source.value != null,則直接給目标類型指派
*/
if (val != null)
{
resultProperty.SetValue(thisValue, Convert.ChangeType(val, resultProperty.PropertyType, null), null);
}
else
{
resultProperty.SetValue(thisValue, Convert.ChangeType(DefaultForType(resultProperty.PropertyType), resultProperty.PropertyType, null), null);
}
}
}
}
}
#endregion
}
/// <summary>
/// 設定屬性
/// </summary>
/// <typeparam name="S"></typeparam>
/// <typeparam name="R"></typeparam>
/// <param name="thisValue"></param>
/// <param name="source"></param>
/// <param name="RfieldNameFunc">要設定屬性的名字,預設是source對象的屬性名對應return對象的屬性名</param>
public static void SetProperties<S, R>(this R thisValue, S source)
{
PropertyInfo[] sourceFields = typeof(S).GetProperties();
#region Set Result Property
if (sourceFields != null)
{
var entityName = typeof(S).Name;
foreach (PropertyInfo sourceProperty in sourceFields)
{
PropertyInfo resultProperty = null;
foreach (var item in typeof(R).GetProperties())
{
string rfieldName = sourceProperty.Name.ToLower();
if (item.Name.ToLower() == rfieldName)
{
resultProperty = item;
break;
}
}
if (resultProperty != null && resultProperty.CanWrite)
{
object val = sourceProperty.GetValue(source, null);
if (resultProperty.PropertyType.IsGenericType && resultProperty.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
/* 如果目标類型是可空類型
* 1. source.value != null, 則用基類型取值,然後給目标類型指派
* 2. source.value = null,則目标類型用預設值null
*/
if (val != null)
{
Type baseType = resultProperty.PropertyType.GetGenericArguments()[0];
resultProperty.SetValue(thisValue, Convert.ChangeType(val, baseType, null), null);
}
}
else
{
/* 目标類型不是可空類型
* 1. source.value = null,則取source.value的預設值給目标類型指派
* 2. source.value != null,則直接給目标類型指派
*/
if (val != null)
{
resultProperty.SetValue(thisValue, Convert.ChangeType(val, resultProperty.PropertyType, null), null);
}
else
{
resultProperty.SetValue(thisValue, Convert.ChangeType(DefaultForType(resultProperty.PropertyType), resultProperty.PropertyType, null), null);
}
}
}
}
}
#endregion
}
/// <summary>
/// 擷取類型的預設值
/// </summary>
/// <param name="targetType"></param>
/// <returns></returns>
private static object DefaultForType(Type targetType)
{
object returnValue = null;
returnValue = targetType == typeof(Int32) ? default(Int32) : returnValue;
returnValue = targetType == typeof(Int64) ? default(Int64) : returnValue;
returnValue = targetType == typeof(Boolean) ? default(Boolean) : returnValue;
returnValue = targetType == typeof(DateTime) ? default(DateTime) : returnValue;
returnValue = targetType == typeof(Double) ? default(Double) : returnValue;
returnValue = targetType == typeof(float) ? default(float) : returnValue;
returnValue = targetType == typeof(Decimal) ? default(Decimal) : returnValue;
returnValue = targetType == typeof(Byte) ? default(Byte) : returnValue;
returnValue = targetType == typeof(Char) ? default(Char) : returnValue;
//returnValue = targetType == typeof(String) ? string.Empty : returnValue;
return returnValue;
}
#endregion
}
如果您覺得本文對你有用,不妨幫忙點個贊,或者在評論裡給我一句贊美,小小成就都是今後繼續為大家編寫優質文章的動力!
歡迎您持續關注我的部落格:)
作者:Ken Wang
出處:http://www.cnblogs.com/Wolfmanlq/
版權所有,歡迎保留原文連結進行轉載:)