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. 多线程之ReentrantReadWriteLock

    java5以后在java.util.concurrent包下,有很多的并发类,可以让我们摆脱java5时,笨重的写法来满足多线程,而且提供了更加丰富的使用场景能力 其中,在locks包下,提供了 Re ...

  2. A little tutorial on CodeFluent Entities with ASP.NET MVC4

    /* Author: Jiangong SUN */ CodeFluent Entities is a model-first development tool which creates non-s ...

  3. 高级屏幕空间反射: Screen Space Reflection (SSR)

    自从CE3首倡SSR以来,发展至今,其质量与当年早已不能同日而语.不仅强调超越性的质量,而且强调超越性的性能.乘着周末有空撸了撸,以下是增强型实时SSR结果图.与我原来的SSR原始实现相比,新的增强型 ...

  4. Scala 深入浅出实战经典 第65讲:Scala中隐式转换内幕揭秘、最佳实践及其在Spark中的应用源码解析

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...

  5. Scala 深入浅出实战经典 第54讲:Scala中复合类型实战详解

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-64讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...

  6. BZOJ 4337: BJOI2015 树的同构 树hash

    4337: BJOI2015 树的同构 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4337 Description 树是一种很常见的数 ...

  7. 面向.Net程序员的后端性能优化实战

    最近2个月没做什么新项目 完全是对于旧的系统进行性能优化 避免超时 死锁 数据处理能力不够等常见的性能问题 这里不从架构方面出发 毕竟动大手脚成本比较高 那么我们以实例为前提 从细节开始 优化角度 一 ...

  8. C#集合--数组

    Array类是所有一维和多维数组的隐式基类,同时也是实现标准集合接口的最基本的类型.Array类实现了类型统一,因此它为所有数组提供了一组通用的方法,不论这些数组元素的类型,这些通用的方法均适用. 正 ...

  9. LINUNX下PHP下载中文文件名代码

            function get_basename($filename){                 return preg_replace('/^.+[\\\\\\/]/', '',  ...

  10. ITF Demo代码(用VBScript构建的接口测试框架)

    ITF Demo代码(用VBScript构建的接口测试框架) http://blog.csdn.net/testing_is_believing/article/details/20872629