天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

自用擴(kuò)展方法分享

image 引言

自從用上擴(kuò)展方法以來,就欲罷不能了,它們大大提升了我的代碼編寫效率,現(xiàn)在我已對(duì)其產(chǎn)生了高度依賴。在此分享一下自己的常用擴(kuò)展方法集,方便大家使用。

(其中有些是借鑒或挪用自其它博友的文章,在此尤其感謝鶴沖天的諸多分享)

源代碼在文章末尾處提供。

示例

public static string ExpandAndToString(this System.Collections.IEnumerable s, string 間隔字符)

功能:將集合展開并分別執(zhí)行ToString方法,再以指定的分隔符銜接,拼接成一個(gè)字符串。

范例:

[TestMethod] public void TestMethod1() {     var i = new int[] {1,5,33,14,556 };     var Out="1-5-33-14-556";     Assert.AreEqual(Out,i.ExpandAndToString("-")); }

 

public static bool IsNullOrEmpty(this string s)

功能:驗(yàn)證字符串對(duì)象是否為空對(duì)象或空字符串。

范例:

[TestMethod]public void TestMethod2(){    string s = null;    Assert.AreEqual(true,s.IsNullOrEmpty());    s += "123";    Assert.AreEqual(false, s.IsNullOrEmpty());}

 

public static string IsNullOrEmptyThen(this string s, System.Func 表達(dá)式)

功能:驗(yàn)證字符串對(duì)象是否為空對(duì)象或空字符串,如果是的話,則執(zhí)行傳入表達(dá)式,并將表達(dá)式結(jié)果返回。

范例:

[TestMethod]public void TestMethod3(){    var s = "";    var Out = "1234";    Assert.AreEqual(Out, s.IsNullOrEmptyThen(f=>"1234"));}

 

public static void IsNullOrEmptyThen(this string s, System.Action 表達(dá)式)

功能:驗(yàn)證字符串對(duì)象是否為空對(duì)象或空字符串,如果是的話,則執(zhí)行傳入表達(dá)式。

范例:

[TestMethod]public void TestMethod4(){    var s = "";    s.IsNullOrEmptyThen(f => MessageBox.Show("無內(nèi)容"));}

 

public static string FormatWith(this string s, params object[] 格式化參數(shù))

public static string FormatWith(this string s, object 格式化參數(shù)1)

public static string FormatWith(this string s, object 格式化參數(shù)1, object 格式化參數(shù)2)

public static string FormatWith(this string s, object 格式化參數(shù)1, object 格式化參數(shù)2, object 格式化參數(shù)3)

功能:格式化字符串。

范例:

[TestMethod]public void TestMethod5(){    var i = 0.35;    var x = 200;    var Out = "i:35%;x:200;";    Assert.AreEqual(Out, "i:{0:0%};x:{1};".FormatWith(i,x));}

 

public static bool In(this T t, params T[] 判斷依據(jù))

功能:判斷當(dāng)前對(duì)象是否位于傳入數(shù)組中。

范例:

[TestMethod]public void TestMethod6(){    var i = 95;    Assert.IsTrue(i.In(31, 3, 55, 67, 95, 12, 4));}

 

public static bool In(this T t, System.Func 判斷表達(dá)式, params C[] 判斷依據(jù))

功能:判斷當(dāng)前對(duì)象是否位于傳入數(shù)組中,判斷方式由傳入表達(dá)式指定。

范例:

[TestMethod]public void TestMethod7(){    var i = 95;    Assert.IsTrue(i.In((c, t) => c.ToString() == t, "31", "3", "55", "67", "95", "12", "4"));}

 

public static bool InRange(this System.IComparable t, T 最小值, T 最大值)

public static bool InRange(this System.IComparable t, object 最小值, object 最大值)

功能:判斷當(dāng)前值是否介于指定范圍內(nèi)。

范例:

[TestMethod]public void TestMethod8(){    var i = 95;    Assert.IsTrue(i.InRange(15, 100));    Assert.IsTrue(i.InRange(-3000, 300));    Assert.IsFalse(i.InRange(-1, 50));    var s = "b";    Assert.IsTrue(s.InRange("a", "c"));    Assert.IsTrue(s.InRange("1", "z"));    Assert.IsFalse(s.InRange("e", "h"));}

 

public static T Trace(this T t)

public static T Trace(this T t, string 分類)

public static T Trace(this T t, System.Func 表達(dá)式)

public static T Trace(this T t, System.Func 表達(dá)式, string 分類)

功能:將當(dāng)前對(duì)象的值輸出到Visual Studio輸出窗口中,并將原始對(duì)象返回。此功能僅用于方便調(diào)試,可以在方法鏈中的任意步驟中輸出值,而不會(huì)對(duì)方法鏈的連續(xù)性有任何影響。

范例:

[TestMethod]public void TestMethod9(){    var s = "abcdefg".Trace(f => f.ToUpper(), "表達(dá)式模式").Remove(4).Trace("普通模式");    var Out = "abcd";    Assert.AreEqual(Out, s);    //輸出內(nèi)容如下:    //表達(dá)式模式: ABCDEFG    //普通模式: abcd}

public static T TraceFormat(this T t, string 格式化字符串)

public static T TraceFormat(this T t, string 格式化字符串, string 分類)

功能:將當(dāng)前對(duì)象的值經(jīng)格式化后輸出到VisualStudio輸出窗口中,并將原始對(duì)象返回。此功能僅用于方便調(diào)試,可以在方法鏈中的任意步驟中輸出值,而不會(huì)對(duì)方法鏈的連續(xù)性有任何影響。

范例:

[TestMethod]public void TestMethod10(){    var m = Math.Max(0.31, 0.65).TraceFormat("Max Value Is {0}", "格式化模式");    var Out = 0.65;    Assert.AreEqual(Out, m);    //輸出內(nèi)容如下:    //格式化模式: Max Value Is 0.65}

 

public static void ForEach(this System.Collections.Generic.IEnumerable source, System.Action 操作)

public static void ForEach(this System.Collections.Generic.IEnumerable source, System.Action 操作)

功能:遍歷一個(gè)集合,執(zhí)行指定操作。(重載形式中,傳入表達(dá)式的int類型參數(shù)代表當(dāng)前循環(huán)次數(shù))

范例:

[TestMethod]public void TestMethod11(){    var l = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };    var c = 0;    l.ForEach(f => c += f);    var Out = 45;    Assert.AreEqual(Out, c);    l.ForEach((f, i) => c -= i);    Out = 9;    Assert.AreEqual(Out, c);}

 

public static Switch Switch(this T v)

public static Case Switch(this T v, System.Func Do)

功能:判斷當(dāng)前值,根據(jù)不同匹配條件執(zhí)行相應(yīng)操作或返回相應(yīng)的值。(重載形式中,允許通過表達(dá)式對(duì)每一次的返回值進(jìn)行疊加處理)
詳細(xì)使用說明參看:《稍加改進(jìn)的Switch/Case擴(kuò)展方法》

范例:

[TestMethod]public void TestMethod12(){    var i = 15;    i.Switch()        .CaseRun(15, f => MessageBox.Show("等于15"),false)        .CaseRun(f => f > 0, f => MessageBox.Show("大于0"))        .CaseRun(f => f < 0, f => MessageBox.Show("小于0"))        .DefaultRun(f => MessageBox.Show("等于0"));    var o = 'c'.Switch()        .CaseReturn('a', 1)        .CaseReturn('b', 2)        .CaseReturn('c', 3)        .CaseReturn('d', 4)        .CaseReturn(f => f > 'd', 5)        .DefaultReturn(0).ReturnValue;    Assert.AreEqual(3, o);}

 

public static System.Collections.Generic.IEnumerable RecursionSelect(this T o, System.Func> 遞歸項(xiàng)選取表達(dá)式)

public static System.Collections.Generic.IEnumerable RecursionSelect(this T o, System.Func> 遞歸項(xiàng)選取表達(dá)式, System.Predicate 檢驗(yàn)表達(dá)式)

功能:遞歸選取項(xiàng)目,并將最終選定的集合返回。
相關(guān)原理說明參看:《c#擴(kuò)展方法奇思妙用高級(jí)篇七:“樹”通用遍歷器》

范例:

[TestMethod]public void TestMethod13(){    //獲取指定目錄中所有包含子目錄的目錄集合    var d = new DirectoryInfo(@"C:/Users/Public/Downloads");    var c = d.RecursionSelect(f => f.GetDirectories(), f => f.GetDirectories().Length > 0);    MessageBox.Show(c.Count().ToString());}

 

public static System.Collections.Generic.IEnumerable RecursionEachSelect(this System.Collections.IEnumerable o, System.Func> 遞歸項(xiàng)選取表達(dá)式)

public static System.Collections.Generic.IEnumerable RecursionEachSelect(this System.Collections.IEnumerable o, System.Func> 遞歸項(xiàng)選取表達(dá)式, System.Predicate 檢驗(yàn)表達(dá)式)

public static System.Collections.Generic.IEnumerable RecursionEachSelect(this System.Collections.Generic.IEnumerable o, System.Func> 遞歸項(xiàng)選取表達(dá)式)

public static System.Collections.Generic.IEnumerable RecursionEachSelect(this System.Collections.Generic.IEnumerable o, System.Func> 遞歸項(xiàng)選取表達(dá)式, System.Predicate 檢驗(yàn)表達(dá)式)

功能:遍歷當(dāng)前集合對(duì)象,逐一遞歸選取項(xiàng)目,并將最終選定的集合返回。
相關(guān)原理說明參看:《c#擴(kuò)展方法奇思妙用高級(jí)篇七:“樹”通用遍歷器》

范例:

[TestMethod]public void TestMethod14(){    //獲取指定目錄中所有包含子目錄的目錄集合    var l = new List<DirectoryInfo>();    l.Add(new DirectoryInfo(@"C:/Users/SkyD/Downloads"));    l.Add(new DirectoryInfo(@"C:/Users/Public/Downloads"));                var c = l.RecursionEachSelect(f => f.GetDirectories(), f => f.GetDirectories().Length > 0);    MessageBox.Show(c.Count().ToString());}

 

public static bool RegexIsMatch(this string s, string 表達(dá)式, System.Text.RegularExpressions.RegexOptions 選項(xiàng))

public static bool RegexIsMatch(this string s, string 表達(dá)式)

public static System.Text.RegularExpressions.Match RegexMatch(this string s, string 表達(dá)式, System.Text.RegularExpressions.RegexOptions 選項(xiàng))

public static System.Text.RegularExpressions.Match RegexMatch(this string s, string 表達(dá)式)

public static System.Text.RegularExpressions.MatchCollection RegexMatches(this string s, string 表達(dá)式, System.Text.RegularExpressions.RegexOptions 選項(xiàng))

public static System.Text.RegularExpressions.MatchCollection RegexMatches(this string s, string 表達(dá)式)

public static string RegexReplace(this string s, string 表達(dá)式, string 替換值, System.Text.RegularExpressions.RegexOptions 選項(xiàng))

public static string RegexReplace(this string s, string 表達(dá)式, string 替換值)

public static string[] RegexSplit(this string s, string 表達(dá)式, System.Text.RegularExpressions.RegexOptions 選項(xiàng))

public static string[] RegexSplit(this string s, string 表達(dá)式)

功能:常用正則表達(dá)式功能封裝,使用方法與Regex類相同。

public static T As(this string s) where T : new(), 通用擴(kuò)展.SpecialString

public static 通用擴(kuò)展.HtmlString AsHtmlString(this string s)

public static 通用擴(kuò)展.PathString ASPathString(this string s)

public static 通用擴(kuò)展.ServerPathString AsServerPathString(this string s)

public static 通用擴(kuò)展.UriString AsUriString(this string s)

public static 通用擴(kuò)展.XHtmlString AsXHtmlString(this string s)

public static 通用擴(kuò)展.XmlString AsXmlString(this string s)

功能:定義為特殊類型的字符串,以使用特有的格式化命令做進(jìn)一步修改。(目前定義后的后續(xù)格式化功能比較有限,以后會(huì)逐步追加)

范例:

[TestMethod]public void TestMethod15(){    var s = @"C:/abc/";    var Out = @"C:/abc/1.exe";    Assert.AreEqual(Out, s.ASPathString().Combine(@"D:/1.exe".ASPathString().FileName));}

 

結(jié)語

這些都是我這里使用頻率最高的擴(kuò)展,希望對(duì)大家也同樣有用:)

下載

擴(kuò)展方法源代碼:http://www.uushare.com/user/icesee/file/2435046

范例源代碼:http://www.uushare.com/user/icesee/file/2435063

本文的XPS版本:http://www.uushare.com/user/icesee/file/2435098

NET技術(shù)自用擴(kuò)展方法分享,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 黄色小说男男 | 黄A无码片内射无码视频 | 国产精品自在拍在线播放 | 高h喷水荡肉爽文总攻 | 国产色精品久久人妻无码看片 | 91精品福利一区二区 | 99re热精品视频国产免费 | 欧美午夜理伦三级在线观看 | 伦理片在线线手机版韩国免费观看 | 99国产视频 | 久久99精品国产自在自线 | 亚洲色欲国产AV精品综合 | 午夜福利理论片在线播放 | 强行撕开衣服捏胸黄文 | 99久久爱看免费观看 | 2019精品国产品在线不卡 | 亚洲欧美日韩另类精品一区二区三区 | 第一次处破女高清电影 | 国内精品伊人久久久影院 | 欧美成人无码视频午夜福利 | 国产午夜三级一区二区三 | 公交车被CAO到合不拢腿 | 久久精品九九亚洲精品天堂 | 人妻免费久久久久久久了 | 伊人久久中文 | 日本一区精品久久久久影院 | 亚洲乱妇88网 | 白丝女仆被啪到深夜漫画 | 免费的黄直播 | 40岁东北老阿姨无码 | 内射后入在线观看一区 | 国产成人拍精品免费视频爱情岛 | 宅男午夜大片又黄又爽大片 | 99re久久热在线播放8 | 免费精品一区二区三区AA片 | 国内极度色诱视频网站 | 日韩欧美一区二区三区免费看 | 日本夜爽爽一区二区三区 | 精品亚洲一区二区在线播放 | 女人一级毛片免费视频观看 | 工口肉肉彩色不遮挡 |