·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> .Net扩展方法集合.

.Net扩展方法集合.

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

     在项目中很多时候都会对字符串和集合做特定的处理。而且很多地方都会去调用。为了解决这些问题。我们通常会在项目中吧这些方法提成公共方法。下面是自己总结的项目中用到的一些扩展方法。封装成了一个Library放到github上.

         https://github.com/FourLeafClover/LightWeight.Extensions

         不多说我们来看看扩展的一些方法吧.

 public static void Main(string[] args)
        {
            #region String Extensions

            string result0 = "[frank%loch".StringConvert4SQLLke(); //替换sql模糊匹配的时候里面的特殊字符
bool result1 = " abC".CurrentTrimEqualsIgnoreCase(" ABC "); // 比较两字符串是否相同,忽略大小写,字符串前后空白
List<string> result2 = "1-23@56*78".SplitByMutipleChar('-','@','*').ToList(); //多字母拆分字符串, 输出 { 1 23 56 78 }的集合
bool result3 = "123".IsNullOrEmpty(); // 判断字符串是否为null或者string.empty.
bool result4 = "123".IsLengthBetween(2, 4);
bool result5 = "123".IsShorterThan(2);
bool result6 = "123".IsLongerThan(2);
bool result7 = "You are the best".IsContainsAll("You", "the");// result==true,result8==false IsContainsAll 支持多参数输入. 用于判断字符串是否同时包含多个字符串 bool result8 = "You are the best".IsContainsAll("You", "123");
bool result9 = "You are the best".IsContainsAny("123", "the"); //result==true,result8==false IsContainsAll 支持多参数输入. 用于判断字符串是否包含多个字符串中的一个

bool result10 = "You are the best".IsContainsAny("sbc", "123");
string result11 = "frank".FirstCharToUpter(); 
string result12 = " frank ".RTrim(); // 去除字符串右边的空白
string result13 = " frank ".LTrim(); // 去除字符串左边空白
string result14 = new List<string>() { "frank", "loch", "hardy" }.ListToSqlIn(false); //用于sql里面的in(x,x,x)语句拼接 string result15 = new List<string>() { "1", "2", "3" }.ListToSqlIn(true); // result12 = "'frank','loch','hardy'",result12 = "1,2,3"
bool result16 = "frank".IsEqualsAnyOfThem("hardy", "frank"); // IsEqualsAnyOfItem支持多参数输入,判断字符串是否在多个删除里面 #endregion #region ConvertExtensions // 用于string类型转换int,decimal,datetime int result17 = "abc".TryToInt32(); int result18 = "123".TryToInt32(); decimal result19 = "1.3".TryToDecimal(); DateTime result20 = "2015/12/24".TryToDateTime(); #endregion #region ObjectExtensions Person p1 = new Person() { Name = "Frank" }; Person result21 = p1.DeepCopy<Person>(); // 深拷贝 #endregion #region List<string> lst = new List<string>() { "123", "456" }; bool result22 = lst.IsNullOrEmpty(); //判断集合是否有内容 bool result23 = lst.CountGreaterThanZero(); List<Person> personList1 = new List<Person>() { new Person(){Name="Frank"}, new Person(){Name="Hardy"} }; List<Person> personList2 = new List<Person>() { new Person(){Name="Frank"}, new Person(){Name="Hardy"} };
///public static bool ObjectCollectionEquals<T>(this IEnumerable<T> collection1, IEnumerable<T> collection2, Func<T, T, bool> func),
   /// func里面写入两个对象相同的方法.实现集合对象的比较 bool result24 = personList1.ObjectCollectionEquals(personList2, (val1, val2) => { return val1.Name != val2.Name; }); #endregion } [Serializable] public class Person { public string Name { get; set; } }

            PS:代码都很简单. 就不剖析了. 各位博友谁还有比较常用的扩展方法都可以提出来. 我好更新到github上.这样可以不断的去完善这个library.