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 <= )
return false; return Array.IndexOf<string>(stringCollect, thisValue) > -;
} /// <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 <= )
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 > )
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 > )
{
result = new string[collection.Count];
for (int i = ; 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 = ; 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()[];
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()[];
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 }

ExtendHelper的更多相关文章

随机推荐

  1. HTTP请求报文和HTTP响应报文(转)

    原文地址:http://blog.csdn.net/zhangliang_571/article/details/23508953 HTTP报文是面向文本的,报文中的每一个字段都是一些ASCII码串, ...

  2. LeetCode:Combination Sum I II

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  3. <后会无期>经典影评

    先说明是转载,任何不同意见请对原作者表达,楼主不作任何回应,楼主影商极低,楼主觉得这二十几年来看的最好的电影是<一代宗师>,楼主只是觉得这篇影评精彩才发布上来让更多的人看到.原作者意见和楼 ...

  4. SQL迁移到ORACLE实例

    nohup ./command.sh > output 2>&1 & SQL迁移到ORACLE实例 日常运维中,我们经常会有数据库不同类型的迁移,比较多的就是从sql se ...

  5. javascript 中的getter,setter

    1.什么是getter,什么是setter? getter 是一种获得属性值的方法,setter是一种设置属性值的方法. 2.怎么定义? 有2种办法: 在对象初始化的时候定义 在对象定义后的时候定义 ...

  6. Ques前端组件化体系

    Ques是一套组件化系统,解决如何定义.嵌套.扩展.使用组件. 传统开发模式的痛点 无法方便的引用一个组件,需要分别引用其Javascript.Template.CSS文件 我们期望能以MV*的方式去 ...

  7. <[你在荒废时间的时候别人都在拼命!]>

    如果我在这里退缩了,那么再也不可能前进 当人有了目标的时候,就会有拼命努力的动力. 当一个人真的掌握了一些东西的时候,才会觉得踏实,这就是所谓的内涵. 人生其实就是这样一步步走过去的.付出总有回报,回 ...

  8. 【转】重新封装FetchUrl函数一枚,支持COOKIES,喜欢领走~!

    mjj520 发表于 2012-6-2 09:14 唉 cpu超级耗芸豆的 查了下开发文档,fetchurl原来是不算CPU的,是我误导了大家.  发表于 2012-6-1 17:30:17 |只看该 ...

  9. JVM -Xss调整Stack Space的大小 【转】

    Java程序中,每个线程都有自己的Stack Space.这个Stack Space不是来自Heap的分配.所以Stack Space的大小不会受到-Xmx和-Xms的影响,这2个JVM参数仅仅是影响 ...

  10. [Compose] 20. Principled type conversions with Natural Transformations

    We learn what a natural transformation is and see the laws it must obey. We will see how a natural t ...